WordPress.org

Ready to get started?Download WordPress

Forums

Custom post type TAXONOMY prev/next links - by tax/category (4 posts)

  1. digitalrenewal
    Member
    Posted 1 year ago #

    I have created a custom post type (products) with dynamic category taxonomies that the client sets up in wordpress.
    Im trying to 'drill down' like this:

    all categories (home.php) > all products in category (taxonomy.php) > single (single-products.php)

    and when you get to a single product, I set it up to cycle through all products in the current category.

    Its all working, except when you click the prev or next post_link, it just clicks through all categories, in order each product was published.

    Is there a way to contain the pagination to the current taxonomy, and start/stop at the end of the posts in that category?

    heres my code:

    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $wp_query->query(array(
    'post_type'=>'products',
    'taxonomy'=> 'product_categories',
    'paged' => $paged,
    'posts_per_page' => 1,
    ));
    
    while ($wp_query->have_posts()) : $wp_query->the_post();
    
    the_post_thumbnail( 'single' );
    previous_posts_link('« Previous');
    $terms = wp_get_post_terms( $post->ID, 'product_categories');
    	foreach ($terms as $term) {
    	echo '<a href="'.get_term_link($term->slug, 'product_categories').'">'.$term->name.'</a>';
    	}
    next_posts_link('Next &raquo;');
    
    the_title();
    the_content();
    endwhile; 
    
    $wp_query = null;
    $wp_query = $temp;  // Reset

    IM SO CLOSE!

  2. vjpo
    Member
    Posted 1 year ago #

    If this code of single.php there is next_post_link instead of next_posts_link(). The last one is for index page and archives navigation.
    next_post_link() has parameter in_same_cat.

    In addition there is some plugins for advanced next/prev navigation:
    Ambrosite Next/Previous Post Link Plus
    Previous and Next Post in Same Taxonomy

  3. bucketpress
    Member
    Posted 1 year ago #

    Hi digitalrenewal, you might want to have a look at this. Had run into the same issue previously and thought I might take a note of it:
    http://bucketpress.com/next-and-previous-post-link-in-same-custom-taxonomy

    And by the way, will be useful to take note of vjpo's comment - the difference between next_post_link and next_posts_link. Though it doesn't actually solve your custom tax issue.

  4. vjpo
    Member
    Posted 1 year ago #

    Smarter Navigation - another good one plugin. Based on browser cookies.

Topic Closed

This topic has been closed to new replies.

About this Topic