【问题标题】:Wordpress: How to show categories and subcategories comma separatedWordpress:如何显示以逗号分隔的类别和子类别
【发布时间】:2017-10-11 14:06:54
【问题描述】:

如何以逗号分隔子类别显示 Wordpress 类别? 我得到了以下内容,但不是每个子类别都用逗号分隔。

我想指定从类别 ID“1”开始的所有内容,以逗号分隔显示所有子类别。

$categories = get_categories();      // Current Category will retrive
foreach ( $categories as $category ) {
$cat = $category->name;              // Parent Category name
$category = get_category_by_slug( $cat );

$args = array(
'type'                     => 'post',
'child_of'                 => $category->term_id,
'orderby'                  => 'name',
'order'                    => 'ASC',
'hide_empty'               => FALSE,
'hierarchical'             => 1,
'taxonomy'                 => 'category',
); 

$child_categories = get_categories($args );

$category_list = array();
$category_list[] = $category->term_id;

if ( !empty ( $child_categories ) ){    // If child Category available
    foreach ( $child_categories as $child_category ){    // Print Child Category
        $category_list[] = $child_category->term_id;
        echo ",". $child_category->cat_name."<br/>";
    }
}
}

【问题讨论】:

    标签: php wordpress


    【解决方案1】:
    $childCategoriesArray=Array(); // just for sake of safety
    
    if ( !empty ( $child_categories ) ){    // If child Category available
        foreach ( $child_categories as $child_category ){    // Print Child Category
            $category_list[] = $child_category->term_id;
            $childCategoriesArray[]=$child_category->cat_name;
        }
    }
    }
    
    echo implode(',',$childCategoriesArray); // that's it - comma separated :)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 2014-11-24
      • 2014-04-09
      • 1970-01-01
      相关资源
      最近更新 更多