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

Automatic Table of contents with categories and posts

programmeradmin0浏览0评论

I don't know if this is a standard feature or if there is already a plugin that does this:

I'd like to have a sidebar widget that produces a table of contents exploding all the categories, their subcategories and all the posts for each category / subcategory, ordered by their publication date. Of course, this can be done with a simple standard menu, but I want it to do this automatically, eg when a new post is published / a new category is added, the menu should automatically reflect that.

Is there any plugin that does this?

I don't know if this is a standard feature or if there is already a plugin that does this:

I'd like to have a sidebar widget that produces a table of contents exploding all the categories, their subcategories and all the posts for each category / subcategory, ordered by their publication date. Of course, this can be done with a simple standard menu, but I want it to do this automatically, eg when a new post is published / a new category is added, the menu should automatically reflect that.

Is there any plugin that does this?

Share Improve this question asked Feb 22, 2018 at 9:03 pistacchiopistacchio 101 2
  • 1 Unfortunately plugin recommendations are off-topic here on WPSE. The functionality is you're after is not part of the WP core, but you could use WP's various APIs to create the functionality that you're after ( widgets, WP_Query, and so on). – Dave Romsey Commented Feb 22, 2018 at 9:35
  • 1 Thanks you very much! I'm programming it, I just wanted to know if there was any ready-made plugin for this :) – pistacchio Commented Feb 22, 2018 at 9:52
Add a comment  | 

1 Answer 1

Reset to default 0

Here is a plugin that does the same.

function get_posts_per_taxterm($taxonomy, $term_id ){
    $posts = get_posts(
        array(
            'posts_per_page' => -1,
            'post_type' => 'post',
            'orderby'  => 'menu_order',
            'order'    => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => $taxonomy,
                    'field' => 'term_id',
                    'terms' => $term_id,
                    'include_children' => false
                )
            )
        )
    );
    // `first_post_id` is a global variable which contains the ID of the post which comes first inside TOC
    // @toDo Find a better way
    global $first_post_id;
    if(!isset($first_post_id)){
        if (!empty($posts)){
            $first_post_id = $posts[0]->ID;
        }
    }
    return $posts;
}
function get_array_of_posts_grouped_by_taxterms($terms, $parent_id = 0, $outputs = []) {
    foreach ($terms as $term) {
        if ($parent_id == $term->parent) {
            $outputs['<a data-id = "'.$term->term_id.'" class = "elementor-kb-term-href stm-content" href ='.get_term_link($term->term_id).'>'.$term->name.'</a>'] = get_array_of_posts_grouped_by_taxterms( $terms, $term->term_id);
            $posts= get_posts_per_taxterm('category',$term->term_id);
            foreach ($posts as $post){
                $outputs['<a data-id = "'.$term->term_id.'" class = "elementor-kb-term-href stm-content" href ='.get_term_link($term->term_id).'>'.$term->name.'</a>'][] = '<a class = "elementor-kb-post-href stm-content" href='.get_permalink($post->ID).' data-id="'.$post->ID.'">'.$post->post_title.'</a>';
            }
        }
    }
    return $outputs;
}
发布评论

评论列表(0)

  1. 暂无评论