杜威分类法的分类规则_如何在父分类法的存档页面上显示子分类法

杜威分类法的分类规则

In the past we have shown you how to display subcategories on category pages in WordPress. Recently while working with Custom Taxonomies, we found a need to display child-taxonomies on parent-taxonomies archive page. After doing a bit of research, we didn’t find a single tutorial covering this issue. In this article, we will show you how to display a list of child taxonomies on taxonomies pages.

过去,我们向您展示了如何在WordPress的类别页面上显示子类别 。 最近,在使用自定义分类法时,我们发现有必要在父分类法档案页面上显示子分类法。 经过一些研究,我们没有找到涵盖此问题的单个教程。 在本文中,我们将向您展示如何在分类法页面上显示子分类法的列表。

Open up your custom taxonomy template file which may look like: taxonomy-{taxonomyname}.php and paste the following code where ever you want to display the list:

打开您的自定义分类模板文件,该文件可能类似于: taxonomy- {taxonomyname} .php ,并将以下代码粘贴到您想要显示列表的位置:


<?php 
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
if ($term->parent == 0) {  
wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&depth=1&show_count=0
&title_li=&child_of=' . $term->term_id);
} else {
wp_list_categories('taxonomy=YOUR-TAXONOMY-NAME&show_count=0
&title_li=&child_of=' . $term->parent);	
}
?>

Don’t forget to replace YOUR-TAXONOMY-NAME with the name of your taxonomy.

别忘了用您的分类名称替换YOUR-TAXONOMY-NAME。

Final Result:

最后结果:

List of Taxonomies

Explanation:

说明:

We are using get_term_by to query the information of the current taxonomy by slug. For example if your taxonomy is called topics and you are on a page /topics/nutrition/ then $term variable will pull all the data related to the specific term page that you are on.

我们使用get_term_by来查询当前分类法的信息。 例如,如果您的分类法称为主题,而您在/ topics / nutrition /页面上,则$ term变量将提取与您所在的特定术语页面相关的所有数据。

In the project we were working on, the topics taxonomy was hierarchical just like categories. So we decided to run a conditional using $term->parent variable. This variable outputs the ID of the parent taxonomy. So if you are on the taxonomy nutrition which is the parent taxonomy, then $term->parent will echo 0. This is why we said if $term->parent == 0 then use wp_list_categories() function to display terms from our custom taxonomy that are child_of the term which page you are on. We accomplished this by using $term->term_id as the child_of variable.

在我们正在研究的项目中,分类法就像类别一样是分层的。 因此,我们决定使用$ term-> parent变量运行条件。 此变量输出父分类法的ID。 因此,如果您使用的是分类法营养(即父分类法),则$ term-> parent将回显0。这就是为什么我们说如果$ term-> parent == 0则使用wp_list_categories()函数显示自定义项中的术语属于您所在页面的术语child_的分类法。 我们通过使用$ term-> term_id作为child_of变量来完成此操作。

Now if you go to the child taxonomy page, it would have been blank because the $term->parent would no longer equals to 0. On a child taxonomy page, $term->parent outputs the ID of the parent category. So we ran an else statement using the same wp_list_categories() function except we changed $term->term_id to $term->parent.

现在,如果转到子分类页面,则该页面将为空白,因为$ term-> parent不再等于0。在子分类页面上,$ term-> parent输出父类别的ID。 因此,我们使用相同的wp_list_categories()函数运行了else语句,除了将$ term-> term_id更改为$ term-> parent。

There you have it. We hope that this helps everyone who was looking for a solution.

你有它。 我们希望这对寻求解决方案的所有人有所帮助。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-display-child-taxonomy-on-parent-taxonomys-archive-page/

杜威分类法的分类规则

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值