仅获取父类别 wordpress
Get only parent categories wordpress
我正在尝试创建类别列表,但我只想列出父类别而不是子类别。我怎样才能做到这一点?到目前为止,我已经创建了一个列表,其中列出了所有父类别和子类别。
function categoryList() {
$args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
$output .= '<ul class="category-list">';
foreach($categories as $category) {
if ($category){
$output .= '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
}
}
$output .= '</li>';
$output .= '</ul>';
return $output;
}
对于父类别,我假设您指的是顶级类别。这实际上记录在 Codex page for get_categories
上:你应该用 parent => 0
调用 get_categories
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0
);
$categories = get_categories($args);
仅列出 top-level(父级)在 wordpress 中的分类。选项 'hide_empty' => 0 确保列出空的 top-level 类别。
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => 0,
//'exclude' => '7',
// optional you can exclude parent categories from listing
);
$categories = get_categories( $args );
return 当前父类别的原生 Wordpress 解决方案,
排除不需要的类别:
function primary_categories($arr_excluded_cats) {
if($arr_excluded_cats == null) {
$arr_excluded_cats = array();
}
$post_cats = get_the_category();
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0
);
$primary_categories = get_categories($args);
foreach ($primary_categories as $primary_category) {
foreach ($post_cats as $post_cat) {
if(($primary_category->slug == $post_cat->slug) && (!in_array($primary_category->slug, $arr_excluded_cats))) {
return $primary_category->slug;
}
}
}
}
//if you have more than two parent categories associated with the post, you can delete the ones you don't want here
$dont_return_these = array(
'receitas','enciclopedico'
);
//use the function like this:
echo primary_categories($dont_return_these);
评论:
- 如果 post 只有一个父类别,请传递 null 而不是数组
- 如果你想要另一个输出而不是 slug,请将其更改为 return $primary_category-> slug;
使用这个:
$categories = get_categories( [ 'parent'=> id_parent ,'hide_empty' => 0,] );
下面的代码会给我们父猫的名字和 URL。
function ns_primary_cat() {
$cat_now = get_the_category();
$cat_now = $cat_now[0];
if ( 0 == $cat_now->category_parent ) {
$catname = '<span class="category"><a href="' . get_category_link( $cat_now->term_id ) . '">' . $cat_now->name . '</a></span>';
} else {
$parent_id = $cat_now->category_parent;
$parent_cat = get_category( $parent_id );
$catname = '<span class="category"><a href="' . get_category_link( $parent_id ) . '">' . $parent_cat->name . '</a></span>';
}
return $catname;
}
我正在尝试创建类别列表,但我只想列出父类别而不是子类别。我怎样才能做到这一点?到目前为止,我已经创建了一个列表,其中列出了所有父类别和子类别。
function categoryList() {
$args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
$output .= '<ul class="category-list">';
foreach($categories as $category) {
if ($category){
$output .= '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
}
}
$output .= '</li>';
$output .= '</ul>';
return $output;
}
对于父类别,我假设您指的是顶级类别。这实际上记录在 Codex page for get_categories
上:你应该用 parent => 0
get_categories
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0
);
$categories = get_categories($args);
仅列出 top-level(父级)在 wordpress 中的分类。选项 'hide_empty' => 0 确保列出空的 top-level 类别。
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => 0,
//'exclude' => '7',
// optional you can exclude parent categories from listing
);
$categories = get_categories( $args );
return 当前父类别的原生 Wordpress 解决方案, 排除不需要的类别:
function primary_categories($arr_excluded_cats) {
if($arr_excluded_cats == null) {
$arr_excluded_cats = array();
}
$post_cats = get_the_category();
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0
);
$primary_categories = get_categories($args);
foreach ($primary_categories as $primary_category) {
foreach ($post_cats as $post_cat) {
if(($primary_category->slug == $post_cat->slug) && (!in_array($primary_category->slug, $arr_excluded_cats))) {
return $primary_category->slug;
}
}
}
}
//if you have more than two parent categories associated with the post, you can delete the ones you don't want here
$dont_return_these = array(
'receitas','enciclopedico'
);
//use the function like this:
echo primary_categories($dont_return_these);
评论:
- 如果 post 只有一个父类别,请传递 null 而不是数组
- 如果你想要另一个输出而不是 slug,请将其更改为 return $primary_category-> slug;
使用这个:
$categories = get_categories( [ 'parent'=> id_parent ,'hide_empty' => 0,] );
下面的代码会给我们父猫的名字和 URL。
function ns_primary_cat() {
$cat_now = get_the_category();
$cat_now = $cat_now[0];
if ( 0 == $cat_now->category_parent ) {
$catname = '<span class="category"><a href="' . get_category_link( $cat_now->term_id ) . '">' . $cat_now->name . '</a></span>';
} else {
$parent_id = $cat_now->category_parent;
$parent_cat = get_category( $parent_id );
$catname = '<span class="category"><a href="' . get_category_link( $parent_id ) . '">' . $parent_cat->name . '</a></span>';
}
return $catname;
}