douyulv6921 2018-11-27 19:23
浏览 41
已采纳

为什么我在wordpress中的类别模板返回的帖子不属于相关类别?

I'm having an issue with my Category template. With a number of categories it's echoing posts that aren't in the category in question. I've checked the posts and categories in my WPAdmin dashboard, they're correct; the problem lies with my code.

Side note: this loop always returns every post in the category, and then some. So it's not missing any, it's just including some that don't belong.

<?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; ?>
  • 写回答

1条回答 默认 最新

  • dsg24156 2018-11-30 18:58
    关注

    Alright, I have the solution. I was getting the post outside the loop, so it was defaulting to the first post. Most posts on this blog have multiple categories, and the ordering wasn't always listing the desired category first. This solution guarantees the category is grabbed based on the page loaded, rather than just the first category of the post:

    <?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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错