포스트 루프에 상위 카테고리 (하위 카테고리 없음) 만 표시하는 방법은 무엇입니까?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
}
}
}
?>
아무 것도 시도해 보지 않으셨습니까? –