• I’ve been searching the Forums and Codex for a few days trying about a half dozen fixes and I can’t seem to find a solution that works. All of the solutions I’m coming across seem to be 2-8 years old as well.

    GOAL:
    I have posts with multiple categories attached to them.
    I wish to be able to select a category, select a post within that category, then have the prev-next buttons on that post work within the selected category only.

    PROBLEM: No matter what I do the prev/next links go to the next chronological post regardless what category I’m in.

    SOLUTIONS TRIED: Adding “TRUE” to the prev_post_link and nav_post_link parameaters. Using different plugins. Trying to write my own code excluding the categories (sample below.) Toolbox puts prev/nav links are in functions.php file although I have tried all my solutions within the single.php file too.Tried switching back and forth permalinks to /%postname%/ and /%category%/%postname%/

    CURRENT SETUP: WP 3.6, theme based on Automattic Toolbox 1.4, home page static, blog page “drawings” with 14 categories. Permalinks set to /%category%/%postname%/

    WEBSITE: https://www.newenglanddrawings.com/drawings

    CURRENT CODE I’m Trying…

    I’m NOT a programmer by any stretch of the imagination, however after reading the codex on the previous_post_link and next_post_link I thought this solution would work. I have 14 categories, so I apologize if this is long. This is what I have in the functions.php page of my theme….

    /**
     * Display navigation to next/previous pages when applicable
     *
     * @since Toolbox 1.2
     */
    function toolbox_content_nav( $nav_id ) {
    	global $wp_query;
    
    	?>
    	<nav id="<?php echo $nav_id; ?>">
    		<h1 class="assistive-text section-heading"><?php _e( 'Post navigation', 'toolbox' ); ?></h1>
    
    	<?php if ( is_single() ) : // navigation links for single posts ?>
    
    	<?php if ( is_page('drawings') ) {
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE );
    	}else if ( in_category('3') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '4,5,6,7,8,9,10,13,14,17,18,19' );
    	}else if ( in_category('4') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,5,6,7,8,9,10,13,14,17,18,19' );
    	}else if ( in_category('5') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,6,7,8,9,10,13,14,17,18,19' );
    	}else if ( in_category('6') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,5,7,8,9,10,13,14,17,18,19' );
    	}else if ( in_category('7') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,5,6,8,9,10,13,14,17,18,19' );
    	}else if ( in_category('8') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,5,6,7,9,10,13,14,17,18,19' );
    		}else if ( in_category('9') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,5,6,7,8,10,13,14,17,18,19' );
    		}else if ( in_category('10') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,5,6,7,8,9,13,14,17,18,19' );
    			}else if ( in_category('13') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,5,6,7,8,9,10,14,17,18,19' );
    	}else if ( in_category('14') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,5,6,7,8,9,10,13,17,18,19' );
    	}else if ( in_category('17') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,5,6,7,8,9,10,13,14,18,19' );
    	}else if ( in_category('18') ){
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,5,6,7,8,9,10,13,14,17,19' );
    	}else if ( in_category('19') ) {
    	previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> Previous Drawing', TRUE, '3,4,5,6,7,8,9,10,13,14,17,18' ); }
    	?>
    
    		<?php if ( is_page('drawings') ) {
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>' );
    	}else if ( in_category('3') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '4,5,6,7,8,9,10,13,14,17,18,19' );
    	}else if ( in_category('4') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,5,6,7,8,9,10,13,14,17,18,19' );
    	}else if ( in_category('5') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,6,7,8,9,10,13,14,17,18,19' );
    	}else if ( in_category('6') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,5,7,8,9,10,13,14,17,18,19' );
    	}else if ( in_category('7') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,5,6,8,9,10,13,14,17,18,19' );
    	}else if ( in_category('8') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,5,6,7,9,10,13,14,17,18,19' );
    		}else if ( in_category('9') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,5,6,7,8,10,13,14,17,18,19' );
    		}else if ( in_category('10') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,5,6,7,8,9,13,14,17,18,19' );
    			}else if ( in_category('13') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,5,6,7,8,9,10,14,17,18,19' );
    	}else if ( in_category('14') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,5,6,7,8,9,10,13,17,18,19' );
    	}else if ( in_category('15') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,5,6,7,8,9,10,13,14,18,19' );
    	}else if ( in_category('18') ){
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,5,6,7,8,9,10,13,14,17,19' );
    	}else if ( in_category('19') ) {
    	next_post_link( '<div class="nav-next">%link</div>', 'Next Drawing <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>', TRUE, '3,4,5,6,7,8,9,10,13,14,17,18' ); }
    	?>
    
    	<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
    
    		<?php if ( get_next_posts_link() ) : ?>
    		<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older Drawings', 'toolbox' ) ); ?></div>
    		<?php endif; ?>
    
    		<?php if ( get_previous_posts_link() ) : ?>
    		<div class="nav-next"><?php previous_posts_link( __( 'Newer Drawings <span class="meta-nav">&rarr;</span>', 'toolbox' ) ); ?></div>
    		<?php endif; ?>
    
    	<?php endif; ?>
    
    	</nav><!-- #<?php echo $nav_id; ?> -->

    I would appreciate any help.
    Thank you.

Viewing 15 replies - 1 through 15 (of 16 total)
  • Hi there,

    I wish to be able to select a category, select a post within that category, then have the prev-next buttons on that post work within the selected category only.

    What you’re trying to do is actually quite challenging technically. ??

    Getting the previous/next buttons to navigate through the posts within only one category is doable. Specifying *which* category that is for posts contained in more than one category is the very tough part. The problem is that the moment you’re on the single post view, WP has no built-in way to “remember” which category you got to that post through.

    This is the code you could use to navigate within a category on a single post. It doesn’t control which category, however, so it only does part of what you’re after:

    <div class="nav-previous"><?php previous_post_link('%link', 'Previous in category', TRUE); ?></div>
    <div class="nav-next"><?php next_post_link('%link', 'Next in category', TRUE); ?></div>

    Those lines would replace this portion of functions.php:

    <?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'toolbox' ) . '</span> %title' ); ?>
    <?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '→', 'Next post link', 'toolbox' ) . '</span>' ); ?>

    p.s. feel free to adjust it to put the div classes inside the PHP code, instead of the way I have it.

    Thread Starter ipreferpencils

    (@ipreferpencils)

    I’ve tried that and it doesn’t work, i’m guessing because I have multiple categories and am on single post view.

    Is this something that would work with tags instead, or with somesort of custom search results?

    Hmm – I actually tested that code on my end and it works on the single view.

    If you go back to your original functions.php file, do you see this portion of the code?

    <?php if ( is_single() ) : // navigation links for single posts ?>
    
    		<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> %title' ); ?>
    		<?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>' ); ?>
    Thread Starter ipreferpencils

    (@ipreferpencils)

    Yes, I see that. I made changes to those two bottom lines of code by adding “TRUE” to the stay in category parameter but it didn’t work for me.

    Thread Starter ipreferpencils

    (@ipreferpencils)

    I went ahead and reloaded the default toolbox functions.php and replaced it with the code you suggested. As with the first time it did not work. It does seem to me this should be farily simple. What’s the point of having categories if you can’t cycle through them?

    Is there a different approach that handles this better? Using TAGS, or perhaps some kind of items manager plugin?

    I went ahead and reloaded the default toolbox functions.php and replaced it with the code you suggested. As with the first time it did not work. It does seem to me this should be farily simple. What’s the point of having categories if you can’t cycle through them?

    Could you please paste the content of your functions.php file into a Pastebin and post a link here so I can investigate?

    Is there a different approach that handles this better? Using TAGS, or perhaps some kind of items manager plugin?

    I don’t know offhand of a plugin that might do this, but it’s something you could look into if you prefer.

    You should definitely be able to navigate through categories on single posts with the standard navigation functions, though, I’ve done it on many sites. If you’d like me to have another look I’m glad to.

    Thread Starter ipreferpencils

    (@ipreferpencils)

    Do you think it’s something to do with Toolbox?
    I’ve never used pastebin before, so I hope I did this right…
    https://pastebin.com/ba863Lps
    Thank you for your help.

    Do you think it’s something to do with Toolbox?

    Since I have this code working on my test site with Toolbox, so I don’t think it’s the theme. ??

    Your code looks OK, thanks for posting it.

    I noticed that your paintings are each included in several categories.

    For example, this painting:
    https://www.newenglanddrawings.com/new-england-scenes/spring-point-ledge-lighthouse/

    …is included in five different categories:

    artist-prints
    featured-drawings
    lighthouses
    new-england-scenes
    oil-pastel-pencil

    When you use the Next/Previous posts links you might be cycling through any one of these five categories, since as I mentioned originally, it’s not possible to specify which one.

    I wonder if that could account for part of the issue here? When you’re cycling through the categories, you may not be “in” the category where you originally clicked the image. If you limited your paintings to a single category each, you would eliminate the issue, but I realize that may not be ideal.

    Another idea – earlier you mentioned tags as a workaround. You may be onto something. What if you assigned each artwork to a single category (for example, Medium), but then used tags to classify them in other ways, like by subject matter?

    Thread Starter ipreferpencils

    (@ipreferpencils)

    I noticed that your paintings are each included in several categories.

    Yes, that is what I have been trying to solve this whole time!

    Another idea – earlier you mentioned tags as a workaround. You may be onto something. What if you assigned each artwork to a single category (for example, Medium), but then used tags to classify them in other ways, like by subject matter?

    That is an excellent idea! I was trying to figure out if tags could be used instead, it didn’t occur to me to combine them… but I don’t know anything about the tags functionality… would i be able to cycle through the same tag even if it was in different categories? How would I change my previous / next links to cycle through the tags instead of the categories?

    Thank you though, you’ve definitely given me something to play around with.

    Thread Starter ipreferpencils

    (@ipreferpencils)

    Ugh, well, I got rid of the multiple categories like you said, I haven’t figured out tags yet but I’ve run into kind of the same problem….

    When on the main drawings page, if i click an image, i want to prev /next navigate through ALL the drawings.

    I only want to navigate through the drawings by category if I get there by category link.

    Is wordpress the wrong tool for this? I’ve heard it’s not really a CMS but a CMS-like blog. Should I be using something else like Modx or Symphony?

    Or is there a way to do this in wordpress without having the traditional 10 posts per page blog scroll?

    Ugh, well, I got rid of the multiple categories like you said, I haven’t figured out tags yet but I’ve run into kind of the same problem….

    The tags would simply give you another way to view groups of artwork. It wouldn’t have an effect on the next/previous links. Those links can only cycle through posts in a category.

    I only want to navigate through the drawings by category if I get there by category link.

    If that’s an absolute requirement, as I mentioned from the start, I don’t think that’s possible in WordPress without major custom programming. You may want to consult a WordPress programmer for an estimate: https://jobs.wordpress.net/

    I’m not familiar with Modx or Symphony – sorry!

    Thread Starter ipreferpencils

    (@ipreferpencils)

    Aha – nice find! Hope that plugin works well for your needs.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Modern Fix for Styaing in Same Category with Post Nav With Multiple Categories?’ is closed to new replies.