Wordpress @ DjangoSpin

WordPress: Displaying posts of a particular category on a custom page

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 2 minutes

Displaying posts of a particular category on a custom page in WordPress

Displaying posts of a particular category on a custom page in WordPress

Today, I wanted to render all posts belonging to a category on a custom page. It's unfortunate that WordPress doesn't provide an easy way to achieve this. I tried WP Posts Filter plugin, but it didn’t seem to work its magic. But the following method worked for me.

Before you proceed, you will need to keep your Page slug (last part of url e.g. www.domain.com/slug) and your Category slug handy, both of which can be found in Pages and Categories options in the left menu bar of WordPress Administrator area.

Now, the main bit:

  1. Navigate to wp-content > themes > your_theme_name
  2. edit page.php as follows
    • place cursor before the opening php tag (<?php) in the while loop line.
    • paste the following code here, replacing the respective slugs:
<?php if( is_page( 'page-slug-here' ) ) { query_posts( array( 'category_name' => 'category-slug-here' ) );
}
>

So, your page.php should look something like:

...
<main id="main" class="site-main" role="main">
<?php if( is_page( 'page-slug-here' ) ) { query_posts( array( 'category_name' => 'category-slug-here' ) );
}
?>
<?php while ( have_posts() ) : the_post(); ?>
...

This should do the trick.

If you want, you could take it a little further, by providing additional parameters to the array function inside query_posts(). For example:

  • you can limit the number of posts to be displayed by: ‘posts_per_page’ => number_here. You could display all posts by putting -1 here.
  • you could sort the posts by date/title by: ‘orderby’ => ‘date’, ‘order’ => ‘ASC’ OR ‘orderby’ => ‘title’, ‘order’ => ‘DESC’.

So your page.php should look like:

...
<main id="main" class="site-main" role="main">
<?php if( is_page( 'page-slug-here' ) ) { query_posts( array( 'category_name' => 'python-programs', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'ASC' ) );
}
?>
<?php while ( have_posts() ) : the_post(); ?>
...

That’s it! Googling ‘query_posts wordpress’ will give you an idea of what all parameters can be passed in the array function.

EDIT: The above method lists all posts of a category on a single page, without pagination. To introduce layout similar to what we get when we click on a category, I ended up discarding the above method, and switched to a plugin named 'Category Redirection for WordPress', available in the wordpress plugin store by the name of 'Redirection' by John Godley. It is very easy to implement, as can be seen here. To reiterate the steps:

  • Go to the Plugin Store by: Go to your WordPress admin area > Hover over Plugins option in the menu > Click on Add New.
  • Look for 'Redirection' by John Godley in the store. Install the plugin.
  • Once installed, in the admin area menu, hover over Tools, and click on Redirection.
  • Look for the form under the heading 'Add new redirection'. Fill the form as follows:
    1. Source URL: /your-category-name/ (You can enter the absolute path to the category here as well, such as www.site.com/your-category-name/. However, it is advised that you enter the relative path.)
    2. Match: URL Only
    3. Action: Redirect to URL
    4. Regular Expression: Unchecked
    5. Target URL: /your-page-slug/ (You can enter the absolute path to the target url here as well, such as www.site.com/your-page-name/. However, it is advised that you enter the relative path.)
  • Click on Add Redirection button. You are good to go!

There you go, now you can view all posts within a category on a custom page, using the Redirection plugin by John Godley.

Hope this helped. Cheers!

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply