从一种自定义帖子类型中过滤 wp_list_categories [英] Filter wp_list_categories from one custom post type

查看:31
本文介绍了从一种自定义帖子类型中过滤 wp_list_categories的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 种自定义帖子类型,称为项目"和客户",它们共享一个称为部门"的分类法.

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>

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

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.

我主要关注项目页面,并希望按我的项目"帖子类型过滤结果.我查看了代码, wp_list_categories 函数似乎不接受参数来执行此操作.

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?

推荐答案

我遇到了类似的问题.我通过克隆 wp_list_categories 函数来做到这一点,给它一个不同的名字,然后在下面一行代码中加入这个代码:$categories=get_categories($r):

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]);
        }
    }

如您所见,它会删除没有您需要的任何帖子类型的类别,然后像 wp_list_categories 正常那样继续该过程.

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.

这篇关于从一种自定义帖子类型中过滤 wp_list_categories的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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