【问题标题】:How to show only Parent Category name in single.php? (wordpress)如何在 single.php 中仅显示父类别名称? (wordpress)
【发布时间】:2015-08-01 13:32:56
【问题描述】:

如何在 single.php 中只打印父类别名称? (wordpress)

我的结构:

A类

-子类别 X

  • 帖子(文章)仅发布到子类别 X。
  • 我只想打印“A 类”的名称。(不带 html 标记)。

【问题讨论】:

    标签: php wordpress-theming wordpress


    【解决方案1】:

    我想你试试这个代码: 对我来说这是工作............

    <?php $parentscategory ="";
    foreach((get_the_category()) as $category) {
    if ($category->category_parent == 0) {
    $parentscategory .= ' <a href="' . get_category_link($category->cat_ID) . '" title="' . $category->name . '">' . $category->name . '</a>, ';
    }
    }
    echo substr($parentscategory,0,-2); ?>
    

    【讨论】:

    • 它对我不起作用。它没有打印任何东西。 :(
    • 我试过这个:&lt;?php echo get_category_parents( get_query_var('cat') , true , '' ); ?&gt; 但我得到:可捕获的致命错误:WP_Error 类的对象无法转换为字符串
    【解决方案2】:
    <?php 
        $get_parent_cats = array(
            'parent' => '0' //get top level categories only
        ); 
        $all_cats = get_categories( $get_parent_cats );
    
        foreach( $all_categories as $single_category ){
    
           $catID = $single_category->cat_ID;
        }
    

    ?>

           echo '<li><a href=" ' . get_category_link( $catID ) . ' ">' . $single_category->name . '</a>'
    

    【讨论】:

    • 请不要只写代码。也提供解释。看看如何answer
    猜你喜欢
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    相关资源
    最近更新 更多