0

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

enter image description here

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?

  • 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 Feb 22 '18 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 Feb 22 '18 at 9:52
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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.