[英]Iterating over correct number of wordpress posts from category + custom post type
我们的wordpress帖子循环结合并显示特定类别和自定义帖子类型的帖子。 这可行,但不会显示所有帖子。 我认为,发布循环是遍历特定类别中的帖子数,而不是特定类别中的帖子数+自定义帖子类型中的帖子数。 如何确保显示正确的帖子数?
<?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(); ?>
您做错了很多事情。
您正在使用页面模板,然后使用the loop
。 这可能拉的所有帖子,作为页面模板,最后回退是index.php
(如在图中看到这里 )。
然后,您在循环中进行了3个附加查询(因此,对于循环的每个帖子,您将进行3个附加查询)。
最后一个查询使用的是query_posts()
,它覆盖了主查询。 只是永远不要使用它。
因此,攻击计划应为:
我想展示什么?
我想在哪里展示?
我需要怎么做才能做到这一点?
开始编写实现此目标所需的东西。
???
利润!!!
如果要控制特定的分类法,请使用$taxonomy.php
模板( $taxonomy
是$taxonomy
的名称)。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.