如何从 Wordpress 的父类别中排除子帖子? [英] How can I exclude child post from its parent category in Wordpress?

查看:26
本文介绍了如何从 Wordpress 的父类别中排除子帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了类别 A,然后是类别 B 和 C,它们都是 A 的子级.当我访问类别 A 时,会列出属于该类别的所有帖子,但它甚至还会显示属于类别 B 和 C 的所有帖子那些帖子没有被分配到 A 类.是否可以只显示 A 类下的帖子并从它的子 B 和 C 中排除所有帖子?我看过很多答案,但其中大部分都是旧的,我不确定它们是否适用于当前版本.另外,我才刚刚开始,即使我已经看到了很多如何克服这个问题的例子,我也很难弄清楚应该在哪里进行更改.
为了实现这一目标,您能指导我在哪里以及需要改变什么吗?预先感谢您的帮助.

I've created category A and then categories B and C which are both child of A. When I visit category A all posts belonging to that category are listed, but it also shows all posts that belongs to category B and C even that those post are not assigned to category A. Is it possible to show only posts under category A and exclude all posts from it childs B and C? I've seen lot of answers but most of then are old and I'm not sure if they work with current version. Also, I'm just starting, and even that I've seen lots of examples of how to overcome this issue it's hard for me to figure where should I make the changes.
Can you guide me in where and what do I need to change in order to achieve this? Thank in advance for your help.

更新

我是新手,还不能回答我自己的问题.但我想分享我的发现以帮助其他有同样问题的人.只需将此代码放在循环之前即可.

I'm new and can't answer my own questions yet. But I wanted to share what I've found to help anyone else with the same issue. Just place it this code before the loop.

  <?php 
      if ( is_category() ) {
      $cat = get_query_var('cat');
      query_posts(array('category__in' => array($cat)));
      }?>

感谢大家的时间和帮助.

Thanks to all for your time and help.

推荐答案

在category.php页面中,把这段代码放在循环之前

In the category.php page just put this code just before the loop

<?php

if ( have_post() ) {
        if (is_category()){
         $category = get_category(get_query_var('cat'));
         $child_cats = array();
     $child_cats = get_term_children($category, 'category');
     query_posts(array('category__not_in' => $child_cats);
         }
   }


 while (have_posts()) : the_post(); $postcount++; ?>

这篇关于如何从 Wordpress 的父类别中排除子帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆