Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Display posts from a specific category in a dropdown list

It is a basic WordPress feature to have categories organized on your blog. While you can easily create new categories, modify them and fill with different posts of your own, delete them or reorganize, displaying posts might be a trickier thing to do.

Yes, WordPress automatically creates pages for each Category on your site. Once you load the page dedicated to a certain category http://www.yourdomain.com/category-name WordPress will instantly load latest posts from it. And while you can choose a category widget which will show all categories, you might be interested in listing all posts from only one category.

Display posts from a Specific Category in a drop-down list. Click to tweet

If you don’t want to deal with additional plugins which can help you organize categories or if you’re working on a new theme, modifying an existing one, etc. you might be interested in the Code we’re about to show you.

This code will take the category ID which you place in the variable and then find all published posts from it. The code will create a dropdown list where a user can easily browse through sorted posts. Once a user chooses a post, clicking on a “Go” button will load it.

Create a drop-down list and load posts from a category:

  1. Open sidebar.php or wherever you want to display the list
  2. Copy and paste the following code:
  3.  $cat_id,
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    ?>
    
  • Change category slug on the 2nd line
  • Save changes
  • Now you can reload your homepage and see that there is a new list added on the sidebar or wherever you put the code. This list will contain all posts from the category you have selected.

    If you like the list and you want to keep it on your site, you can go on and start modifying the looks of it via CSS code.

    The post Display posts from a specific category in a dropdown list appeared first on WP Loop.



    This post first appeared on WP Loop, please read the originial post: here

    Share the post

    Display posts from a specific category in a dropdown list

    ×

    Subscribe to Wp Loop

    Get updates delivered right to your inbox!

    Thank you for your subscription

    ×