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

编程入门 行业动态 更新时间:2024-10-18 00:19:23
本文介绍了从一种自定义帖子类型中过滤 wp_list_categories的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 09:35:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1407735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   类型   帖子   wp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!