wordpress 父页面_如何在WordPress发布循环中仅显示父类别

wordpress 父页面

When you use the_category() template function in your WordPress theme, it displays both the parent category and any child/subcategories under it. If you are creating a site that uses a lot of child categories to categorize the content, then this can become a problem because the_category code will create a lot of clutter. For example, it will show as “Filed in Blogs, Genesis Framework, 2 Column Theme etc”. Unfortunately the_category() does not have any optional parameters like child=0 or depth=-1. When creating our WordPress Gallery, this was one of the issues we had to deal with. In this article, we will show you how to display only parent category in your WordPress post loop.

当您在WordPress主题中使用the_category()模板函数时,它将同时显示父类别和其下的所有子类别。 如果要创建一个使用大量子类别对内容进行分类的网站,则这会成为问题,因为the_category代码会造成很多混乱。 例如,它将显示为“归档在博客, Genesis Framework中 ,两栏主题等”。 不幸的是, the_category()没有任何可选参数,例如child = 0或depth = -1。 在创建WordPress Gallery时 ,这是我们必须处理的问题之一。 在本文中,我们将向您展示如何在WordPress发布循环中仅显示父类别。

Simply open your single.php file or anywhere else that you want to utilize this code, and then replace the following code or something similar to it:

只需打开您的single.php文件或您想使用此代码的其他任何地方,然后替换以下代码或类似的代码:

<?php the_category(', '); ?>

with this code:

使用此代码:

<?php $parentscategory ="";
foreach((get_the_category()) as $category) {
if ($category->category_parent == 0) {
$parentscategory .= ' <a href="' . get_category_link($category->cat_ID) . '" title="' . $category->name . '">' . $category->name . '</a>, ';
}
}
echo substr($parentscategory,0,-2); ?>

Now this will let you display only the parent category in your WordPress post loop. For example see the image in the example:

现在,这将使您仅在WordPress发布循环中显示父类别。 例如,请参见示例中的图像:

Display Only Parent Category in Your WordPress Loop

Our issue with the gallery was not so much clutter, but rather it was to make sure our Site Submission Form powered by Gravity Forms can capture all the data and store it as a Post Draft, so it can make our editors job a lot easier. Unfortunately, Gravity Forms does not support Custom Taxonomy because it would have saved us the trouble, but due to the lack of that feature, we had to use the code above. Each of our gallery articles have two categories associated with it (Parent Category: Genre of Site, and Child Category: Theme Framework it uses). In our next article, we will share how you can display only Child Category in your WordPress Post Loop. Stay updated, and make sure you submit your WordPress site to our WordPress Gallery.

我们与画廊有关的问题并不是那么混乱,而是要确保由Gravity Forms提供支持的“ 网站提交表单 可以捕获所有数据并将其存储为后期草稿,这样可以使我们的编辑工作更加轻松。 不幸的是, Gravity Forms不支持自定义分类法,因为它可以为我们省去麻烦,但是由于缺少该功能,我们不得不使用上面的代码。 我们每个画廊的文章都有两个相关类别(“父类别:网站类型”和“子类别:主题框架”)。 在我们的下一篇文章中,我们将分享如何在WordPress帖子循环中仅显示子类别。 保持更新,并确保将您的WordPress网站提交到我们的WordPress Gallery

References:

参考文献:

the_categoryEriculous

the_category 认真的

翻译自: https://www.wpbeginner.com/wp-themes/how-to-display-only-parent-category-in-your-wordpress-post-loop/

wordpress 父页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值