我的类别模板有问题。对于许多类别,它回显了不在相关类别中的帖子。我已经检查了WPAdmin仪表板中的帖子和类别,它们是正确的;问题出在我的代码上。
旁注:此循环始终返回类别中的每个帖子,然后返回一些。因此,这并不是缺少,而是包括一些不属于的东西。
<?php
$categories = get_the_category(); $category_id = $categories[0]->cat_ID;
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1, 'cat' => $category_id )); ?>
<?php if ( $wpb_all_query->have_posts() and !empty($category_id) ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><br/><?php echo get_the_date('l, F j, Y'); _e(' by ');?><a href="<?php echo home_url(); ?>/author/<?php echo the_author_meta('nicename'); ?>"><?php echo get_author_name(); ?></a></p>
<p><br/><?php _e('Categories: '); echo the_category( '/' ); ?></p>
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
<span><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?><br/><a href="<?php echo the_permalink();?>">Continue Reading ></a></span>
<p><br/><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<p><?php echo get_the_date('l, F j, Y'); _e(' by ');?><a href="<?php echo home_url(); ?>/author/<?php echo the_author_meta('nicename'); ?>"><?php echo get_author_name(); ?></a></p>
</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
答案 0 :(得分:0)
好的,我有解决方案。我将帖子发布到循环之外,因此它默认为第一篇帖子。该博客上的大多数帖子都有多个类别,并且排序并不总是首先列出所需的类别。此解决方案可确保根据加载的页面(而不只是帖子的第一个类别)获取类别:
<?php
$url = $_SERVER['REQUEST_URI'];
preg_match('/\/category\/(.+)\/.*/', $url, $matches);
$category_slug = $matches[1];
$category_id_actual = get_category_by_slug($category_slug)->term_id;
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1, 'cat' => $category_id_actual )); ?>
<?php if ( $wpb_all_query->have_posts() and !empty($category_id_actual) ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><br/><?php echo get_the_date('l, F j, Y'); _e(' by ');?><a href="<?php echo home_url(); ?>/author/<?php echo the_author_meta('nicename'); ?>"><?php echo get_author_name(); ?></a></p>
<p><br/><?php _e('Categories: '); echo the_category( '/' ); ?></p>
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
<span><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?><br/><a href="<?php echo the_permalink();?>">Continue Reading ></a></span>
<p><br/><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<p><?php echo get_the_date('l, F j, Y'); _e(' by ');?><a href="<?php echo home_url(); ?>/author/<?php echo the_author_meta('nicename'); ?>" class="authorname"><?php echo get_author_name(); ?></a></p>
<div style="float:right;"></div>
</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>