admin管理员组文章数量:1434907
I have multiple custom post types, viz., research, documents, booklets. I have a single archive template for each of them , archive-research.php, archive-documents.php, archive-booklets.php respectively.
I need another template where I display posts from all these CPTs.
For a single CPT, I used the following code:
query_posts(
array(
'post_type' => 'post_type_name_here',
'posts_per_page' => 4,
'paged'=>$paged
)
);
Is there any way to merge theme?
I have multiple custom post types, viz., research, documents, booklets. I have a single archive template for each of them , archive-research.php, archive-documents.php, archive-booklets.php respectively.
I need another template where I display posts from all these CPTs.
For a single CPT, I used the following code:
query_posts(
array(
'post_type' => 'post_type_name_here',
'posts_per_page' => 4,
'paged'=>$paged
)
);
Is there any way to merge theme?
Share Improve this question edited Apr 8, 2019 at 22:07 Sven 3,6841 gold badge35 silver badges48 bronze badges asked Apr 8, 2019 at 21:29 Sunday LalbiakniaSunday Lalbiaknia 111 silver badge2 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 4As Jacob Peattle mentioned, you should not be using query_posts in your custom archive templates, that's redundant as WP is already going to query those posts for you. What you really need is pre_get_posts
(https://codex.wordpress/Plugin_API/Action_Reference/pre_get_posts). That will allow you to conditionally modify the parameters of the main query before it executes. Additionally since you are effectively executing the same query for all 4 CPTs, then it's unnecessary to have 4 separate templates to do so, instead take a look at the template_include
filter https://codex.wordpress/Plugin_API/Filter_Reference/template_include
Add the following to your functions file...
//Modify the main query
function custom_archive_query($query){
if(is_admin() || !$query->is_main_query()){
return;
}
$cpts = array("research","documents","booklets");
if(is_post_type_archive($cpts)){
$query->set('post_type', $cpts);
return;
}
}
add_action('pre_get_posts', 'custom_archive_query');
//Add the template redirect
function custom_archive_template($template){
$cpts = array("research","documents","booklets");
if(is_post_type_archive($cpts)){
$new_template = locate_template( array( 'custom_archive-template.php' ) );
if(!empty($new_template)) return $new_template;
}
return $template;
}
add_filter('template_include', 'custom_archive_template');
As an additional note, you may need to adjust your query more than this example, and obviously your custom post type names to match. Your original query is paging at 4 posts per page, that may have undesired results due to the fact that you're combining multiple post types.
本文标签: Merge multiple custom post types in a single archive template
版权声明:本文标题:Merge multiple custom post types in a single archive template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745612611a2666203.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
query_posts()
. WordPress will query the correct posts for you. – Jacob Peattie Commented Apr 9, 2019 at 1:22WP_Query
with thepost_type
key having an array with all your post types. – Karun Commented Apr 9, 2019 at 5:14post_type
. Thanks – Sunday Lalbiaknia Commented Apr 9, 2019 at 6:21'post_type' => array('viz', 'research', 'documents', 'booklets'),
– Karun Commented Apr 9, 2019 at 6:23