Reputation: 1421
I'm using ACF to create pages with separate flexible layout sections in two areas, the main page section area and a sidebar area.
I have a CPT called case-studies
and have created a page called case-studies
. I have set 'has_archive' => false,
within the CPT in the hope that the page is used however (and I understand) that there is a hierarchy to this so that if no archive is given, then it will load my home.php (which is what I am using for my news, the posts post type).
Is there any way for a page to be used as an archive for a CPT? The only solution I can see at the moment is to use the ACF CPT Options Pages plugin and attached the ACF fields I have created to my 'Case Studies Archive' page however, I would have to duplicate all of my code so that they can called the right content. For example:
the_field('field_name');
to the_field('field_name', 'cpt_case-studies');
Has anyone been able to use a page as an archive for a CPT and assign a template to the page which both queries the CPT posts and have the ACF fields from the page?
Thanks very much for your help. If there is anything else I can supply, let me know.
Upvotes: 1
Views: 1638
Reputation: 1421
Building on Daniel James' answer, I created a function which has an array for all of the pages that I need to get the ID of to add their content to specific archive pages.
function getOtherPageID($page = null)
{
global $post;
$postIDs = [
'case-studies' => 42, // 42 being the page ID of the content I need
];
if(array_key_exists($page, $postIDs)) :
$id = $postIDs[$page];
endif;
return $id;
}
By calling $id = getOtherPageID('case-studies');
within my archive-case-studies.php
template page (or any other archive template), I'm able to grab the ID I need and then add this to the ACF fields. Then in every corresponding ACF field call, I would simply add the $id
to the ACF field call: get_field('field_name', $id);
Hope this helps others out.
Instead of typing in the post ID (in the case above, 42), I have amended this to use the get_page_by_path();
function. Therefore, my function is:
function getOtherPageID($page = null)
{
global $post;
$postIDs = [
'case-studies' => get_page_by_path( $page ),
];
if(array_key_exists($page, $postIDs)) :
$id = $postIDs[$page]->ID;
endif;
return $id;
}
This will now get the post object and then $id
will be set to the post object's ID and will then be returned.
Upvotes: 0
Reputation:
The best approach for this is to have the has_archive
parameter set to true
and then create an archive-case-studies.php
template in your theme. You can still make use of custom fields from ACF. When you use the_field()
and the_get()
, the second accepted parameter is the post id, but this is automatically set based on the current global object.
For example:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_field( 'custom_field', $post->ID ); ?>
<?php endwhile; endif; ?>
Is the same as:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_field( 'custom_field' ); ?>
<?php endwhile; endif; ?>
Long story, short; use the archive page with a custom template and you'll be able to achieve what I think you're after without an issue. If you do it this way, it's a lot less effort involved than rolling something custom as archive pages can be a pain to work with.
Upvotes: 2