如何在後循環中只顯示父類別(沒有子類別)?WordPress的:在後循環上只顯示父類別
我:
<span class="entry-category"><?php the_category(', '); ?></span>
我該怎麼辦?
如何在後循環中只顯示父類別(沒有子類別)?WordPress的:在後循環上只顯示父類別
我:
<span class="entry-category"><?php the_category(', '); ?></span>
我該怎麼辦?
foreach (get_the_category() as $cat) {
$parent = get_category($cat->category_parent);
if (!get_category($cat->category_parent)) {
// do something with the category, like use it to create a new query_posts or something
}
}
它不適用於我 – user3383480
請嘗試下面的答案... –
您可以獲得類別並檢查每個條目的parent
字段;
<?php
$categories = get_the_category();
if($categories){
foreach($categories as $category) {
if ($category->parent < 1) {
// Your action here
}
}
}
?>
你嘗試過什麼嗎? –