繁体   English   中英

wordpress get_categories功能不起作用

[英]wordpress get_categories function not working

我正在尝试从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个类别。 我想我做错了什么:(

它会工作,

  $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); 
    }

你能查看任何类别的任何帖子吗。如果任何帖子没有任何类别,那么没有类别显示。

如果要显示所有类别而不将帖子分配到类别。 更改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>";
?>

这是我的代码工作。

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);

使用<?php get_taxonomies( $args, $output, $operator ) ?> get_categories可能适用于post_type='post'

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM