如何获得wordpress类别slug,包括Parent

时间:2014-10-25 09:25:52

标签: wordpress

我在下面的代码中显示了获取当前类别的slug,我还希望它获得该类别的父级(如果有的话)...

<?php
 if (is_category( )) {
   $cat = get_query_var('cat');
   $yourcat = get_category ($cat);
   echo '<link rel="canonical" href="http://www.xxxxx.com/'. $yourcat->slug.'/">';
 }
 ?>

我希望父类别适合使用/如上所述。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码获取父类别slug:

<?php
 if (is_category( )) {
   $cat = get_query_var('cat');
   $yourcat = get_category ($cat);
   echo '<link rel="canonical" href="http://www.xxxxx.com/'. $yourcat->slug.'/">';


     $category_parent_id = $yourcat->parent;
     $category_parent = get_term( $category_parent_id, 'category' );
    echo $parent_category_slug = $category_parent->slug;

 }
 ?>
相关问题