[英]Iterating over correct number of wordpress posts from category + custom post type
Our wordpress post loop combines and displays posts from a specific category and a custom post type. 我们的wordpress帖子循环结合并显示特定类别和自定义帖子类型的帖子。 This works, but is not displaying all posts.
这可行,但不会显示所有帖子。 I believe that the post loop is iterating over the number of posts in the specific category, not the number of posts in the specific category + the number of posts in the custom post type.
我认为,发布循环是遍历特定类别中的帖子数,而不是特定类别中的帖子数+自定义帖子类型中的帖子数。 How can I ensure that the correct number of posts are being displayed?
如何确保显示正确的帖子数?
<?php
/*
Template Name: Articles & Cases
*/
get_header(); ?>
<div class="center-holder">
<div id="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title( '<h1>', '</h1>' ); ?>
<?php the_post_thumbnail( 'full' ); ?>
<?php the_content(); ?>
<?php if ( $cats = get_field( 'category' ) ) : ?>
<?php
$args = array(
'post_type' => array( 'post' ),
'category__in' => $cats,
'fields' => 'ids',
);
$articles = new WP_Query( $args );
wp_reset_postdata();
$args = array(
'post_type' => array( 'case_study' ),
'fields' => 'ids',
);
$case_study = new WP_Query( $args );
wp_reset_postdata();
$all_posts_ids = array_merge( $articles->posts, $case_study->posts );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'post', 'case_study' ),
'post__in' => $all_posts_ids,
'paged' => $paged,
);
query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php get_template_part( 'blocks/pager' ); ?>
<?php else: ?>
<?php get_template_part( 'blocks/not_found' ); ?>
<?php endif; wp_reset_query(); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php get_sidebar( 'blog' ); ?>
</div>
<?php get_footer(); ?>
There is a number of things you're doing wrong. 您做错了很多事情。
You are using a page template, and then are looping using the loop
. 您正在使用页面模板,然后使用
the loop
。 This probably pulls all posts, as the last fallback for page template is index.php
(as seen in the diagram here ). 这可能拉的所有帖子,作为页面模板,最后回退是
index.php
(如在图中看到这里 )。
Then you are making 3 additional queries while in the loop (so for every post you loop you make 3 extra queries). 然后,您在循环中进行了3个附加查询(因此,对于循环的每个帖子,您将进行3个附加查询)。
And the last query, you are using query_posts()
which is overriding the main query. 最后一个查询使用的是
query_posts()
,它覆盖了主查询。 Just don't ever use that. 只是永远不要使用它。
So the plan of attack should be: 因此,攻击计划应为:
What do I want to show? 我想展示什么?
How and where do I want to show it? 我想在哪里展示?
What do I need to do to achieve this? 我需要怎么做才能做到这一点?
Start writing things needed to achieve this. 开始编写实现此目标所需的东西。
??? ???
Profit!!! 利润!!!
If you want to control specific taxonomy, use $taxonomy.php
template (with $taxonomy
being the name of the taxonomy.). 如果要控制特定的分类法,请使用
$taxonomy.php
模板( $taxonomy
是$taxonomy
的名称)。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.