[英]How to get all post categories without custom post type categories?
I am creating a Blog in wordpress.我正在 wordpress 中创建博客。
I have a list of categories: Technology, Art, fashion, Home, Lifetime, Education, Business, Religion, Design and home, Marketing我有一个类别列表:技术、艺术、时尚、家庭、终身、教育、商业、宗教、设计和家庭、营销
In which some of these categories I am using only in Custom Post Type (Technology, Art, Fashion) and others only in Normal Posts (Home, Life, Education, Business, Religion, Design and home, Marketing).其中一些类别我仅在自定义帖子类型(技术、艺术、时尚)中使用,而其他类别仅在普通帖子(家庭、生活、教育、商业、宗教、设计和家庭、营销)中使用。
Now I need to get the list of categories that are only being used from normal posts to show them on my blog ().现在我需要获取仅从普通帖子中使用的类别列表,以便在我的博客上显示它们()。 I tried to do the following but it returns all categories including CPTs:
我尝试执行以下操作,但它返回所有类别,包括 CPT:
$categories = get_categories();
foreach($categories as $category) {
echo '<li class="cat-name" . '>' . $category->name . '</li>';
}
I just need to show the categories: Home, Life, Education, Business, Religion, Design & Home, Marketing.我只需要显示类别:家庭、生活、教育、商业、宗教、设计与家庭、营销。
And exclude those that are being used in CPT.并排除那些在 CPT 中使用的。 Please Help!
请帮忙!
You need to pass in the arguments and exclude the term_ids or include on the term_ids you want.您需要传入 arguments 并排除 term_ids或包含在您想要的 term_ids 上。 You can use one of the following: 'exclude', 'exclude_tree', or 'include'.
您可以使用以下之一:“exclude”、“exclude_tree”或“include”。
$args = array(
'taxonomy' => 'category',
'exclude' => array(65,23,98,23,78), // term_ids you want to exclude
'exclude_tree' => (65,23,98,23,78), // term_ids you want to exclude and their descendants/children
'include' => (11,51,90,57,29), // only the term_ids you want to include
'orderby' => 'name',
'order' => 'ASC',
"hide_empty" => 1,
);
$cats = get_categories($args);
foreach ($cats as $cat){
echo $cat_slug = $cat->slug;
}
'include' '包括'
Array or comma/space-separated string of term IDs to include.要包含的术语 ID 的数组或逗号/空格分隔的字符串。 Default empty array.
默认空数组。
'exclude' '排除'
Array or comma/space-separated string of term IDs to exclude.要排除的术语 ID 的数组或逗号/空格分隔的字符串。 If $include is non-empty, $exclude is ignored.
如果 $include 不为空,则忽略 $exclude。 Default empty array.
默认空数组。
'exclude_tree' '排除树'
Array or comma/space-separated string of term IDs to exclude along with all of their descendant terms.要排除的术语 ID 及其所有后代术语的数组或逗号/空格分隔的字符串。 If $include is non-empty, $exclude_tree is ignored.
如果 $include 不为空,则忽略 $exclude_tree。 Default empty array.
默认空数组。
Learn more about all the arguments you can pass to get_categories() or get_terms() , get_posts() etc https://developer.wordpress.org/reference/classes/wp_term_query/__construct/了解有关您可以传递给get_categories()或get_terms() 、 get_posts() 等的所有 arguments的更多信息。
Try WP_Query if the above isn't working如果上述方法不起作用,请尝试WP_Query
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'home', 'life', 'education', 'business', 'religion', 'design-and-home', 'marketing' ),
'operator' => 'IN'
),
array(
'taxonomy' => 'custom_taxonomy_registerd_to_cpt_slug', // again make sure in your cpt plugin that this taxonomy is unique and not just 'category'
'field' => 'slug',
'terms' => array( 'technology', 'art', 'fashion' ),
'include_children' => false,
'operator' => 'NOT IN'
)
)
);
$cats = new WP_Query($args);
foreach ($cats as $cat){
print_r($cat); // use this to find the values you need. Remove after you build the link html
}
This function with shortcode is tested and work for me这个带有简码的 function 已经过测试并为我工作
<?php
function list_categories_func($atts) {
$a = shortcode_atts( array('' => '',), $atts );
$args = array('taxonomy'=>'category','parent'=>0,'orderby'=>'name','order'=>'ASC', 'hide_empty'=>0,);
$categories=get_categories($args);
echo "<ul>";
foreach($categories as $category){
$name=$category->slug;
echo "<li>$id</li>";
}
echo "</ul>";
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode( 'ni_categories_row', 'list_categories_func' );
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.