[英]Wordpress ACF: Using a page (with a specific page template) as a CPT archive page
I'm using ACF to create pages with separate flexible layout sections in two areas, the main page section area and a sidebar area. 我正在使用ACF在两个区域(主页区域和侧边栏区域)中创建具有单独的灵活布局部分的页面。
I have a CPT called case-studies
and have created a page called case-studies
. 我有一个称为case-studies
的CPT,并创建了一个名为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). 我在CPT中设置了'has_archive' => false,
以希望使用该页面,但是(据我了解)该页面具有层次结构,因此,如果未提供任何存档,则它将加载我的home.php (这是我用于新闻的帖子类型)。
Is there any way for a page to be used as an archive for a CPT? 是否可以将页面用作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. 目前,我唯一能看到的解决方案是使用ACF CPT Options Pages插件并将我创建的ACF字段附加到“案例研究档案”页面,但是,我必须重复所有代码,以便它们可以调用正确的内容。 For example: 例如:
the_field('field_name');
to the_field('field_name', 'cpt_case-studies');
到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? 有没有人能够将页面用作CPT的存档并为该页面分配一个模板,该模板既查询CPT帖子又具有该页面的ACF字段?
Thanks very much for your help. 非常感谢您的帮助。 If there is anything else I can supply, let me know. 如果还有其他我可以提供的信息,请告诉我。
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. 最好的方法是将has_archive
参数设置为true
,然后在主题中创建一个archive-case-studies.php
模板。 You can still make use of custom fields from ACF. 您仍然可以使用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. 当您使用the_field()
和the_get()
,第二个接受的参数是帖子ID,但这是根据当前全局对象自动设置的。
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. 如果以这种方式进行操作,则与滚动自定义内容相比,所需的工作量要少得多,因为使用存档页面可能会很麻烦。
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. 在Daniel James的答案的基础上,我创建了一个函数,该函数为所有需要获取ID的页面(将其内容添加到特定存档页面)提供了一个数组。
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');
通过调用$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. 在我的archive-case-studies.php
模板页面(或任何其他存档模板)中,我可以获取所需的ID,然后将其添加到ACF字段中。 Then in every corresponding ACF field call, I would simply add the $id
to the ACF field call: get_field('field_name', $id);
然后,在每个相应的ACF字段调用中,我只需将$id
添加到ACF字段调用中: 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();
我没有输入帖子ID(在上面的示例中为42),而是对其进行了修改,以使用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. 现在,这将获取post对象,然后将$id
设置为post对象的ID,然后将其返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.