简体   繁体   English

存在多个自定义分类法时,Get_terms返回错误

[英]Get_terms returns error when more than one custom taxonomy exists

Hi people from StackOverflow! 嗨,来自StackOverflow的人!

I'm having a problem while using the get_terms function: 使用get_terms函数时出现问题:

I have a website to design, with two levels of categories. 我有一个网站设计,分为两个级别。 For the first one I use the already existing Wordpress taxonomy "category", and for the second level, I created a custom taxonomy. 对于第一个,我使用现有的Wordpress分类法“类别”,对于第二个级别,我创建了一个自定义分类法。

When the user loads a page, it gets all the custom post_type "content" and filters the results with the two levels of taxonomies set in the post, to show only the needed content. 当用户加载页面时,它将获取所有自定义的post_type“内容”,并使用帖子中设置的两级分类法过滤结果,以仅显示所需的内容。

I integrated all the content of my custom taxonomy's first occurrence, but when I added a new one, the previous ones were not working anymore: 我整合了自定义分类法首次出现的所有内容,但是当我添加一个新分类法时,以前的分类法不再起作用:

get_terms(); get_terms(); returned the name of my new category even for the posts who were not tagged with it, in the page of the second taxonomy, and get_terms() returned an empty array in the first category's page. 在第二个分类法的页面中,即使对于没有标签的帖子也返回我的新类别的名称,而get_terms()在第一个类别的页面中返回一个空数组。

When I deleted the second category, the problem disappeared and all the posts of the first category started working again. 当我删除第二个类别时,问题消失了,并且第一个类别的所有帖子都重新开始工作。

The problem may come from the fact that instead of coding the custom taxonomy by myself, I used the "Simple Taxonomy" plugin, but no one seemed to really have problems with it on the internet, so I'm not sure if it comes from it. 问题可能出在以下事实:我不是使用自己编写的自定义分类法,而是使用了“简单分类法”插件,但是似乎没有人真的在互联网上遇到问题,因此我不确定它是否来自它。

I'll show you the part where I get the error: 我将向您展示出现错误的部分:

<ul id="contentList">
    <?php query_posts('post_type=contenus&showposts=-1');
    while ( have_posts() )
        {
            the_post();
            $rubrique = get_terms('rubrique');
                if (ucwords($actual_rubrique) == $rubrique[0]->name){
                    echo '<a href="';
                    $url = explode('?', $_SERVER['REQUEST_URI']);
                    echo $url[0];
                    echo '?tab=';
                    echo $post->ID;
                    echo '"><li>';
                    echo the_title();
                    echo '</li></a>';};?>
    <?php }; ?>
</ul>

When it works, get_terms() returns "Urbanisme" for the Urbanism category for example. 当它起作用时,例如,对于Urbanism类别,get_terms()返回“ Urbanisme”。 (It's a french website) (这是一个法国网站)

EDIT: 编辑:

I tried to change my way of thinking: Instead of doing all this on a cutsom post_type page, I moved it to page.php. 我试图改变自己的思维方式:我没有将它放在cutsom post_type页面上,而是将其移至page.php。 I've still got the same problem: 我仍然有同样的问题:

When I just have one term, every page on the website is tagged with it, and when there are two, they all take the second one, even those who were checked to be in the first... 当我只有一个术语时,网站上的每个页面都会被加上标签,而当有两个页面时,它们都占据了第二个,即使那些被选中的页面也是如此。

I really don't understand what's happening. 我真的不明白发生了什么。 Please help :/ 请帮忙 :/

Problem solved: 问题解决了:

I didn't understand how get_terms() worked. 我不明白get_terms()是如何工作的。 It just returned me an array of ALL the existing terms of my taxonomy. 它只是返回了我分类法中所有现有术语的数组。

To get the actual terms I used has_terms(): 为了获得我使用过的has_terms()的实际术语:

  if (has_term($actual_rubrique, 'rubrique', $post)){ do_stuff();};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM