在Wordpress中仅显示父类别

时间:2014-05-28 07:49:47

标签: php wordpress

我创建了要在首页中显示的类别和子类别列表。当试图显示我的所有类别和子类别都列出。我只想列出我的主要类别。如果有人可以帮助解决这个问题。谢谢

<span class="ad-category">
<?php
$category = get_the_category();

if ($category[0]->category_parent == 0) {
    $tag = $category[0]->cat_ID;

    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
    if (isset($tag_extra_fields[$tag])) {
        $category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
        $category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
    }
} else {
    $tag = $category[0]->category_parent;

    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
    if (isset($tag_extra_fields[$tag])) {
        $category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
        $category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
    }

}

if(!empty($category_icon_code)) {
    ?>

    <div class="category-icon-box" style="background-color: <?php echo $category_icon_color; ?>;"><?php $category_icon = stripslashes($category_icon_code); echo $category_icon; ?></div>

    <?php
} 

$category_icon_code = "";
?>
</span>

1 个答案:

答案 0 :(得分:0)

试试这个

<span class="ad-category">
<?php
$category = get_the_category();
if ($category[0]->category_parent == 1) {
   $tag = $category[0]->category_parent;

    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
    if (isset($tag_extra_fields[$tag])) {
    $category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
    $category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
    }
} 
if(!empty($category_icon_code)) {
    ?>

    <div class="category-icon-box" style="background-color: <?php echo $category_icon_color; ?>;"><?php $category_icon = stripslashes($category_icon_code); echo $category_icon; ?></div>

    <?php
} 

$category_icon_code = "";
?>
</span>