• GOAL: I just want to display previous and next post links for the posts under a specific category, on a dedicated page for that category. I don’t want to show any other posts from other categories.

    WHAT I’VE DONE: I’ve created a new page template, a new loop template and an archives template. I’ve set up the loop to use wp_query and grab the category.

    <?php
    
    $args = array( 'category_name' => 'comics', 'posts_per_page' => 1 );
    
    $loop = new WP_Query( $args );
    
    ?>
    
    <?php if ( $loop->have_posts() ) while ( $loop->have_posts() ) : $loop->the_post(); ?>
    				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    						<h2 class="entry-title" style="text-align:center"><?php the_title(); ?></h2>
    						<hr/>
    					<div class="entry-content">
    						<?php the_content(); ?>
    						<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
    					</div><!-- .entry-content -->
    				</div><!-- #post-## -->
    
    <?php endwhile; // end of the loop. ?>

    QUESTION: How would I create links for the previous and next posts in this category?

Viewing 4 replies - 1 through 4 (of 4 total)
  • First of all, consider whether you need to use a custom query for this loop. It might turn out that you do for your particular case, but usually you can set up a regular loop inside a file and name it category-[name].php, where [name] is the slug of your category. WordPress will then automatically know to use this template only for the desired category.

    See: http://codex.wordpress.org/Template_Hierarchy

    For adding Prev/Next buttons, you want to take advantage of the WP function paginate_links(). Here’s a function that I use on my own personal blog that uses this:


    function my_pagination() {
    global $wp_query;

    $total_pages = $wp_query->max_num_pages;

    if ($total_pages > 1){

    $current_page = max(1, get_query_var('paged'));

    echo paginate_links(array(
    'base' => get_pagenum_link(1) . '%_%',
    'format' => '/page/%#%',
    'current' => $current_page,
    'total' => $total_pages,
    'prev_next' => 'true'
    ));
    }
    }

    Then just drop my_paginate_links(); in your template OUTSIDE of the loop.

    Hope that helps.

    Thread Starter Cameron Roe

    (@cjroe)

    I’m deciding if it would be better to use a custom post type. Considering I only want to display 1 post at a time, I could use singe-{custom-post-type}.php for the actual page and include links for previous post in that category. Would this work any better?

    That might make it a bit more complicated because you’ll need to set up a custom taxonomy for the post type to serve as the categories.

    Assuming you stick with regular posts, what you’d want to do is stick with a category loop that displays only one post per page, using ‘posts_per_page’ (see: http://codex.wordpress.org/Class_Reference/WP_Query
    )
    If you use paginate_links() on a single page it will paginate through all posts & won’t be limited to a specific category.

    WP-PageNavi works very nicely for this. Example in use I simply replaced twentyeleven’s default prev next links code with

    <?php wp_pagenavi(); ?>

    in category.php (and other template files in the child theme)

    It can be customized in the plugin settings and styled in your stylesheet.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Prev and Next Post links for specific category page’ is closed to new replies.