Lowest catagory link

时间:2014-09-18 作者:ashraf

我正在尝试显示最低的子类别链接。此时,我只能显示子类别名称,但我希望它带有永久链接。到目前为止,我的方法

$categories = get_the_category($post->ID);

foreach($categories as $category) :
    $children = get_categories( array (\'parent\' => $category->term_id ));
    $has_children = count($children);

    if ( $has_children == 0 ) {
        echo $category->name;
    }
endforeach;
它显示最低类别名称,但不显示指向该类别的超链接。如何链接类别?

1 个回复
最合适的回答,由SO网友:ashraf 整理而成

至少完全做到了

$categories = get_the_category($post->ID);

foreach($categories as $category) :

    $children = get_categories( array (\'parent\' => $category->term_id ));
    $category_link = get_category_link($children);
    $has_children = count($children);

    if ( $has_children == 0 ) {
        $name=$category->name;
        $category_link = get_category_link($category);
        echo "<a href=\'$category_link\'>$name</a>";
    }
endforeach;

结束

相关推荐

Show Pages in Categories

通过将此代码添加到函数中,我创建了category函数。php:function page_category() { register_taxonomy_for_object_type(\'category\', \'page\'); } // Add to the admin_init hook of your theme functions.php file add_action( \'init\', \'page_category\' ); 但问