【问题标题】:In Wordpress how can I display only the child-categories and not the parents using wp_list_categories在 Wordpress 中,如何使用 wp_list_categories 仅显示子类别而不显示父母
【发布时间】:2014-12-27 18:19:54
【问题描述】:

好的,我有他们的类别和子类别。

分类:

本田

-思域

-雅阁

丰田

-相机

-科罗拉

日产

-最大值

-反面

现在我想使用 wp-list-categories 随机显示其中的 5 个 Sub_Categories。我经历了所有的参数,但仍然无法弄清楚。

所以我想随机看看:

  • 反之
  • 公民
  • 相机
  • 千里马
  • 科罗拉

(不显示本田、日产等的父级)

如何做到这一点?或者如果它不能与 wp_list_categories 那么有更好的选择吗?

【问题讨论】:

    标签: php html css wordpress wp-list-categories


    【解决方案1】:

    使用get_terms 可能是更好的方法:

    // get all terms
    $terms = get_terms( 'category' );
    
    // shuffle!
    shuffle( $terms );
    
    foreach( array_slice( $terms, 0, 5) as $term ){
    
        // skip top level terms
        if( 0 == $term->parent )
            continue;
    
        echo '<a href="' . get_term_link( $term, 'category' ) . '">' . $term->name . '</a>';
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2014-04-09
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    相关资源
    最近更新 更多