Developers Diary

WordPress Snips to Keep for Later

When using WordPress custom PAGE template, to pull POSTS of a certain category, and then get those posts to paginate:
(first pull the normal page data, then add a 2nd query to pull the posts, make sure the pagination is inside the loop.  I get it… but for some reason ONLY this line of data worked for me)

<div id=”categoryList”>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>

<?php endwhile; else: ?>
<p><?php _e(‘Sorry, this page does not exist.’); ?></p>
<?php endif; ?>

<?php // THIS IS HOW TO GET PAGINATION, use the very next line:
$wp_query = new WP_Query( array ( ‘cat’ => ‘5’, ‘posts_per_page’ => ‘5’, ‘paged’ => get_query_var( ‘paged’ ) ) );
if (have_posts()) : while (have_posts()) : the_post();?>
<h3 class=”articletitle”><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></h3>
<p class=”time”><?php the_time(‘l, F jS, Y’); ?></p>
<?php the_excerpt();
endwhile; ?>

<!– pagination –>
<p class=”later”><?php next_posts_link(); ?></p>
<p class=”earlier”><?php previous_posts_link(); ?></p>
<?php else: ?>
<p><?php _e(‘Sorry, this page does not exist.’); ?></p>
<?php endif; ?>

</div>