WordPress menu that shows all posts in a certain category

I am working on creating a widget menu(kind of a sidebar like w3schools) that is supposed to show all posts that are listed in a particular category.
For example from the home page once the user clicks “HTML” in the Nav bar it takes them to the page with introduction about the course and I have put a sidebar custom menu(by using drag and drop) in which there are other posts of the same category by the name of “HTML”.

I want to be able to make a post and mark it under “HTML” category, in the menu option I select the menu to show menu only on posts with the “HTML” category and then after that whenever I make a post under the category “HTML”, the menu should automatically add another post to the list. Simply put, a dynamic menu that shows all posts under a certain category.

Read More

So far I have come across this piece of code but putting it in the editor for menu doesn’t work :

$catPost = get_posts(get_cat_ID("NameOfTheCategory"));

foreach ($catPost as $post) : setup_postdata($post); ?>
        <div>
             <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
              <p><?php the_content(); ?></p>
        </div> <?php  endforeach;?>

I am new to WordPress and I have tried searching everywhere and trying a lot of plugins but I can’t find anything.

Related posts

1 comment

  1. you can get all the categories or a specific category using get categories and then run a WP_Query on each category.

        //menu loop
    function menu_loop() {
    
    global $post;
    
    $args= array(...);
    $categories = get_categories( $args);
    foreach ($categories as $category ) {
    
    $args = array (...);
    
    // The Query
    $query = new WP_Query( $args );
    
        // The Loop
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) { $query->the_post();
    
            }
        }
       // Restore original Post Data
        wp_reset_postdata();
    }
    }
    

Comments are closed.