【问题标题】:wordpress get_categories function not workingwordpress get_categories 功能不起作用
【发布时间】:2016-01-21 05:04:58
【问题描述】:

我正在尝试从 custom post 类型的 category 分类中检索类别 通过以下代码:

$categories = get_categories(array(
        'type'        => 'ad_listing',
        'child_of'    => 0,
        'parent'      => '',
        'orderby'     => 'name',
        'order'       => 'ASC',
        'hide_empty'  => 1,
        'hierarchical'=> 1,
        'exclude'     => '',
        'include'     => '',
        'number'      => '',
        'taxonomy'    => 'ad_cat',
        'pad_counts'  => false 
    ));
    echo"<pre>";
    print_r($categories);
    echo"</pre>";

但它没有在类别中显示任何内容,尽管有 3 个类别。 我想我做错了什么:(

【问题讨论】:

  • 你有这些类别的帖子吗? hide_empty 没有显示没有附加帖子的类别。

标签: php wordpress categories


【解决方案1】:

它会起作用的,

  $cat_args = array(
                      'parent'  => 0,
                      'hide_empty' => 0,
                      'order'    => 'ASC',
                   );
    $categories = get_categories($cat_args);
    foreach($categories as $category){
       echo get_cat_name($category->term_id); 
    }

【讨论】:

    【解决方案2】:

    您能否检查任何类别中的任何帖子。如果任何帖子不属于任何类别,则没有类别显示。

    如果您想显示所有类别而不将帖子分配给类别。更改 hide_empty=>false 见下面代码

    <?php
    $categories = get_categories( array(
                'type'        => 'post',
                'child_of'    => 0,
                'parent'      => '',
                'orderby'     => 'name',
                'order'       => 'ASC',
                'hide_empty'  => false,
                'hierarchical'=> 1,
                'exclude'     => '',
                'include'     => '',
                'number'      => '',
                'taxonomy'    => 'category',
                'pad_counts'  => false 
            ) );
        echo"<pre>";
        print_r($categories);
        echo"</pre>";
    ?>
    

    【讨论】:

    • 您好,我的问题有误。应该是自定义帖子类型。请再次查看问题
    • 您的代码为我工作。如果您在没有附加到类别的情况下获取所有类别,请更改 'hide_empty' => false,
    【解决方案3】:

    这是适合我的代码。

    global $wpdb;
    $all_cats = array();
    $args = array(
        'taxonomy' => 'product_cat',
        'orderby' => 'name',
        'field' => 'name',
        'order' => 'ASC',
        'hide_empty' => false
    );
    $all_cats = get_categories($args);
    echo '<pre>';
    print_r($all_cats);
    

    【讨论】:

    • 您好,我的问题有误。应该是自定义帖子类型。请再次查看问题
    【解决方案4】:

    使用&lt;?php get_taxonomies( $args, $output, $operator ) ?&gt; get_categories 可能用于post_type='post'

    https://codex.wordpress.org/Function_Reference/get_taxonomies

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 2018-03-10
      • 1970-01-01
      相关资源
      最近更新 更多