仅在WordPress中显示Top类别



我有一些属于多个类别的帖子。我已经为他们每个人定义了一个顶级类别,我想只在我的类别页面上显示这个顶级类别。不幸的是,它显示了所有类别的属性。(如。这个类别页面上的第一篇文章:https://www.la-strada.net/rubrique/muzik-zak/)。一个开发人员提供了一个功能,应该有帮助,但这是一个很长的机会…

https://gist.github.com/LucasDemea/afa5d58c8e9ed0d71b7b2523d84bcce9

我的观点是所使用的主题(Qode Bridge)使用自己的术语来命名WP元素,因此代码不能正确地调用它们。对此有什么想法吗?

你链接的脚本实际上是工作的,但是你必须在前端调用它。

如果你想在循环中更直接地获取它们,你可以这样做:

<?php
if( have_posts() ): // ... loop starting
while( have_posts() ): the_post();
// ... get our post categories
$categories = get_the_category(); // ... get post categories
foreach( $categories as $category ) {
if ( $category->parent == 0 ) { // ... if the category has no parent
echo '<p>' . $category->cat_name . '</p>'; // ... display the category name
};
};
the_title( '<h1>', '</h1>' );
endwhile;
endif; // ... loop ending
?>
功能描述
get_the_category检索文章类别。

最新更新