If we have a custom WordPress article classification, how can we operate it separately if needed.
function wp_list_categories_for_post_type($post_type, $args = '') {
$exclude = array();
//Retrieve the classification of articles of the specified article type (or custom article type)
foreach (get_categories() as $category) {
$posts = get_posts(array('post_type' => $post_type, 'category' => $category->cat_ID));
//If not found
if (empty($posts))
//Add category ID to exclusion list
$exclude[] = $category->cat_ID;
}
//Set query parameter arg
if (! empty($exclude)) {
$args .= ('' === $args) ? '' : '&';
$args .= ' exclude='.implode(',', $exclude);
}
//Display category
wp_list_categories($args);
}
Select CodeCopy
Add to functions.php.
For example, if there is a custom article type called movie, obtain the classification of the movie.
wp_list_categories_for_post_type('movie');
Select CodeCopy
Then call it like this:
p_list_categories_for_post_type('movie','order=DESC&title_li=Cats');
- WordPress, 调用WordPress, WordPress自定义文章分类
- 1 Users Found This Useful