Is there a plugin to display certain posts on certain pages? [closed]

On the homepage I want to display the latest 4 posts from the ‘news’ and ‘events’ category, whereas on the events page, I just want to display all posts in the ‘events’ category in WP’s usual paginated form if there is a lot of them.

Do you know of any plugin that handles this kind of rules based post generation? If not, what would be the best way of going around this?

2 Answers
2

you should maybe think about using one of WordPress’ built in queries for this sort of thing and create yourself a couple of custom loops. There probably are plugins out there that can do this for you but as a general rule it’s better to reduce reliance on 3rd party scripts wherever possible.

To do what you have described above you’d probably need a loop that looks like this (Drop this into your homepage template):

// This is where we set up the parameters for your custom loop.  In the example below you would want to swap out the category ID numbers with the IDs for your News and Events cateogries
<?php $my_query = new WP_Query( 'cat=2,6&post_per_page=4' );?>

// The Loop
<?php if($my_query->have_posts()) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

//Add template tags and other stuff here that you want to show up for each post

<?php endwhile; else: ?>
    <p>Sorry - No posts to display.</p>
<?php endif; wp_reset_query();?>

To achieve your other loop you would want pretty much the same thing as above, however, you will need to change the first line a bit. Something like this should work (drop this into your custom page template):

//This adds in the pagination that you require.  
//Once again, you will need to modify the category ID to match the ID of the one you want to display.  
//You can also tweak the other parameters to suit your requirements
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( array( 'cat' => '12', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC', 'paged' => $paged ) ); ?>

More info on WP-Query can be found here:

http://codex.wordpress.org/Class_Reference/WP_Query

Hope this helps!

Leave a Comment