从一种自定义帖子类型中过滤 wp_list_categories

发布于 2024-10-21 12:49:37 字数 1611 浏览 3 评论 0原文

我有两种名为“项目”和“客户”的自定义帖子类型,它们共享一个名为“部门”的分类法。

if (!is_taxonomy('sector')) {
        register_taxonomy(
        'sector', array('project', 'client'), array(
        'hierarchical' => true,
        'label' => 'Sector',
        'query_var' => true,
        'rewrite' => array( 'slug' => 'sector' ),
        'with_front' => false
        ) );

        wp_insert_term('Health', 'sector');
        wp_insert_term('Clubs', 'sector');
        wp_insert_term('Commercial', 'sector');     
    }

我创建了一个带有侧栏导航的分类存档模板,其中列出了我的分类存档的链接,使用:

//list terms in a given taxonomy using wp_list_categories 

    $orderby      = 'name'; 
    $show_count   = 1;      // 1 for yes, 0 for no
    $pad_counts   = 0;      // 1 for yes, 0 for no
    $hierarchical = 0;      // 1 for yes, 0 for no
    $show_option_none='';
    $title        = '';

    $args_sector = array(
      'taxonomy'     => 'sector',
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical, 
      'title_li'     => $title
    );


<ul id="sideNav" class="rightSubMenu">   
      <h3 class="rightSubNav">SECTOR</h3>
      <ul id="sideNav" class="rightSubMenu">
        <?php wp_list_categories( $args_sector ); ?>
      </ul>

</ul>

问题是,如果我有一个链接到“俱乐部”的项目和链接到“俱乐部”的客户端,则输出计数显示 2存档页面还显示 2 个帖子 - 1 个用于项目,1 个用于客户。但只有一个项目。

我主要关心项目页面,并想按我的“项目”帖子类型过滤结果。我查看了 codex,发现 wp_list_categories 函数似乎不接受执行此操作的参数。

有人可以帮忙吗?有更好的方法吗?

I have 2 custom post types called 'project' and 'client' that share a taxonomy called 'sector'.

if (!is_taxonomy('sector')) {
        register_taxonomy(
        'sector', array('project', 'client'), array(
        'hierarchical' => true,
        'label' => 'Sector',
        'query_var' => true,
        'rewrite' => array( 'slug' => 'sector' ),
        'with_front' => false
        ) );

        wp_insert_term('Health', 'sector');
        wp_insert_term('Clubs', 'sector');
        wp_insert_term('Commercial', 'sector');     
    }

I have created a taxonomy archive template with a sidebar nav that lists links to my taxonomy archives using:

//list terms in a given taxonomy using wp_list_categories 

    $orderby      = 'name'; 
    $show_count   = 1;      // 1 for yes, 0 for no
    $pad_counts   = 0;      // 1 for yes, 0 for no
    $hierarchical = 0;      // 1 for yes, 0 for no
    $show_option_none='';
    $title        = '';

    $args_sector = array(
      'taxonomy'     => 'sector',
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical, 
      'title_li'     => $title
    );


<ul id="sideNav" class="rightSubMenu">   
      <h3 class="rightSubNav">SECTOR</h3>
      <ul id="sideNav" class="rightSubMenu">
        <?php wp_list_categories( $args_sector ); ?>
      </ul>

</ul>

The problem is if I have a project that is linked to 'clubs' and a client that is linked to 'clubs' the output count shows 2. Also the archive page shows 2 posts - 1 for project and one for client. But there is only one project.

I am mainly concerned with the project page and would like to filter the results by my 'project' post type. I looked through the codex and the wp_list_categories function doesn't seem to accept a parameter to do this.

Can anyone help? Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2024-10-28 12:49:37

我也遇到过类似的问题。我通过克隆 wp_list_categories 函数,给它一个不同的名称,并在行后放入此代码来完成此操作: $categories=get_categories($r):

 foreach ($categories as $key => $category){
        $temp = array ( 'post_type'=>$r['type'], 'tax_query' => array(
            array (
                'taxonomy' => $category->taxonomy,
                'field' => 'slug',
                'terms' => $category->slug
            )

        )
            );
        $pauli = new wp_query($temp);
        if($pauli->post_count==0){
            unset($categories[$key]);
        }
    }

如您所见,它删除了不具有您的任何帖子类型的类别需要,然后像 wp_list_categories 通常那样继续该过程。

I have had a similar problem. I did this by cloning the wp_list_categories function, giving it a different name and putting in this code after the line: $categories=get_categories($r):

 foreach ($categories as $key => $category){
        $temp = array ( 'post_type'=>$r['type'], 'tax_query' => array(
            array (
                'taxonomy' => $category->taxonomy,
                'field' => 'slug',
                'terms' => $category->slug
            )

        )
            );
        $pauli = new wp_query($temp);
        if($pauli->post_count==0){
            unset($categories[$key]);
        }
    }

As you can see, it removes categories that do not have any of the post type you need, and then continues the process as wp_list_categories does normally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文