如何获取当前帖子的父类别标签


12

我的主题使用以下代码按类别进行样式设置,该代码将当前类别的子段插入为CSS类。

<div class="CategorySpecificStyle 
    <?php $category = get_the_category(); echo $category[0]->slug; ?>">
        <?php echo $category[0]->cat_name; ?>
</div> 

现在,我将要添加大量新的子类别,并且当我应该能够仅选择当前帖子的父类别并对其应用样式时,将它们全部添加到CSS中似乎很愚蠢。

我已经能够获得父类别的名称:

$parentcat = get_cat_name($category[0]->category_parent);

但是空格(和大小写)是一个问题……而且我似乎无法理解父类别的意思。

我知道我可能在某个地方缺少一个简单的步骤,但是任何见解将不胜感激。

Answers:


18

您将需要使用返回的ID值$category[0]->category_parent并将其传递给get_term()。例:

$category = get_the_category(); 
$category_parent_id = $category[0]->category_parent;
if ( $category_parent_id != 0 ) {
    $category_parent = get_term( $category_parent_id, 'category' );
    $css_slug = $category_parent->slug;
} else {
    $css_slug = $category[0]->slug;
}

5

您将需要查询父类别数据。get_category为此而构建。

$category = get_the_category(); 
$parent = get_category($category[0]->category_parent);
echo $parent->slug;

这将返回该类别的直接父级。给出以下类别集合:

  • 动画片
      • 史酷比

如果您为其指定“ Scooby”的ID,则上面的代码将返回“ Dog”。如果您想要最顶层的父类别“卡通”,则无论嵌套的深度如何,都可以使用以下方法:

$category = get_the_category(); 
$parent = get_ancestors($category[0]->term_id,'category');
if (empty($parent)) {
  $parent[] = array($category[0]->term_id);
}
$parent = array_pop($parent);
$parent = get_category($parent); 
if (!is_wp_error($parent)) {
  var_dump($parent);
} else {
  echo $parent->get_error_message();
}

这也具有相对整齐的错误处理的优点。


感谢您的回答,将来我可能会使用类似的代码段,但如果该帖子在父类别/类别中没有子类别,则也会引发错误。
DLR 2014年

@DLR:是的,我知道。我必须离开才能完成答案。我正在研究更复杂,更强大的工具。参见编辑。
s_ha_dum 2014年

0

我喜欢@s_ha_dum的上一个答案,但是为了获得不分深度的顶级类别,我使用了我认为是更简单的解决方案:

$cats = get_the_category();
foreach ( $cats as $cat ) {
    if ( $cat->category_parent == 0 ) {
        return $cat->name; // ...or whatever other attribute/processing you want
    }
}
return ''; // This was from a shortcode; adjust the return value to taste

0

如果它可以帮助别人......让虐猫或父母,视01您提上$category

$category = get_the_category();
$parent = get_cat_name( $category[0]->category_parent );
if( ! function_exists('get_cat_slug') )
{
    function get_cat_slug($cat_id) {
        $cat_id = (int) $cat_id;
        $category = &get_category($cat_id);
        return $category->slug;
    }
}
if ( !empty($parent) ) {
    $output .= '<H2>' . esc_html( $category[1]->name ) . '</H2>';                               
} else {
    $output .= '<H2>' . esc_html( $category[0]->name ) . '</H2';
}

0

您可以像这样简化它:

  $category   = get_the_category();
  $cat_parent = $category[0]->category_parent;
  $category   = $cat_parent != 0 ? get_term($cat_parent, 'category')->slug : $category[0]->slug;

0

以下函数适用于返回类别:

function get_root_category($category = null) {
  if (!$category) $category = get_the_category()[0];
  $ancestors = get_ancestors($category->term_id, 'category');
  if (empty($ancestors)) return $category;
  $root = get_category(array_pop($ancestors)); 
  return $root;
}

用法: get_root_category()->slug

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.