最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Changing a function in function.php to a shortcode - for listing categories of only a certain post type

programmeradmin1浏览0评论

I'm trying to create a shortcode that can display a list of categories. However, it should:

  • First, limit the results to a specific post type (based on shortcode usage)
  • then check to see if category has any posts, if it doesn't, omit it from the list
  • Finally, display list in alphabetical order

I found some code online that seems to work, if I want to use it as a page template, but I would like to use it as a shortcode so I can place it in a widget on my site.

For example, I would like to type: [catlist post_type=places] and it will display a list of categories under the post type "Places".

Here is the code that is sort of working, however it places the list outside of the widget, almost in a div before the widget box.

function wp_list_categories_for_post_type($post_type, $args = '') {
    
    $exclude = array();

    // Check ALL categories for posts of given post type
    foreach (get_categories() as $category) {
        $posts = get_posts(array('post_type' => $post_type, 'category' => $category->cat_ID));

        // If no posts found, ...
        if (empty($posts))
            // ...add category to exclude list
            $exclude[] = $category->cat_ID;
          
    }

    // Set up args
    if (! empty($exclude)) {
        $args .= ('' === $args) ? '' : '&';
        $args .= 'exclude='.implode(',', $exclude);
    }

    // List categories
    wp_list_categories($args);

}
add_shortcode('catlist', 'wp_list_categories_for_post_type');

Thank you for anyone that can help.

I'm trying to create a shortcode that can display a list of categories. However, it should:

  • First, limit the results to a specific post type (based on shortcode usage)
  • then check to see if category has any posts, if it doesn't, omit it from the list
  • Finally, display list in alphabetical order

I found some code online that seems to work, if I want to use it as a page template, but I would like to use it as a shortcode so I can place it in a widget on my site.

For example, I would like to type: [catlist post_type=places] and it will display a list of categories under the post type "Places".

Here is the code that is sort of working, however it places the list outside of the widget, almost in a div before the widget box.

function wp_list_categories_for_post_type($post_type, $args = '') {
    
    $exclude = array();

    // Check ALL categories for posts of given post type
    foreach (get_categories() as $category) {
        $posts = get_posts(array('post_type' => $post_type, 'category' => $category->cat_ID));

        // If no posts found, ...
        if (empty($posts))
            // ...add category to exclude list
            $exclude[] = $category->cat_ID;
          
    }

    // Set up args
    if (! empty($exclude)) {
        $args .= ('' === $args) ? '' : '&';
        $args .= 'exclude='.implode(',', $exclude);
    }

    // List categories
    wp_list_categories($args);

}
add_shortcode('catlist', 'wp_list_categories_for_post_type');

Thank you for anyone that can help.

Share Improve this question edited Jul 2, 2020 at 8:34 Tako Bubble asked Jun 29, 2020 at 12:21 Tako BubbleTako Bubble 312 bronze badges 1
  • Your output is happening before the widget code completes. wordpress.stackexchange/questions/73082/… – jdm2112 Commented Jun 29, 2020 at 14:14
Add a comment  | 

1 Answer 1

Reset to default 0

It looks like wp_list_categories($args) will output directly when you call it, which is why the output comes out in a weird place. and ideally what you need is to capture the output and return it, but luckily wp_list_categories allows you to do this with the 'echo' parameter. Try:

$args['echo'] = FALSE;
return wp_list_categories($args);

Note the extra 'return'

发布评论

评论列表(0)

  1. 暂无评论