Support » Fixing WordPress » Using categories as if they were separate blogs

  • Resolved anoctave

    (@anoctave)


    For various and sundry reasons, I was posting on several different blogs and recently decided to bring them together under one wordpress site. I have three distinct categories and I want to keep their posts from intermingling, but it’s nor working that way.

    For instance, when one clicks on the earliest post in Category A, the previous post link takes you to the last post in Category B and the next post link takes you to the first post in Category C.

    The code in the archive,php file is

    <ul class="pager">
    						<?php if( get_previous_posts_link() ) : ?>
    							<li class="previous"><?php previous_posts_link(); ?></li>
    						<?php endif; ?>
    						<?php if( get_next_posts_link() ) : ?>
    							<li class="next"><?php next_posts_link(); ?></li>
    						<?php endif; ?>
    						</ul>

    I found an article online that had code that would segregate the previous-next by category:

    <ul class="pager">						
    <?php if(get_previous_post_link( $format, $link, $in_same_term = true, $excluded_terms = '', $taxonomy = 'category' ) )  ?>
        <li class= "previous"><?php previous_posts_link(); ?></li>
    <?php enddif; ?>
    <?php if (get_next_posts_link( $format, $link, $in_same_term = true, $excluded+terms = '', $taxonomy = 'category' ) ) ?>
        <li class= "next"><?php next_posts_link();  ?></li>
    <?php endif; ?>
    </ul>

    I took a copy of archive.php from my parent theme, edited with the second set of code and tried it. I got an error.

    So obviously something is wrong and I am not knowledgeable enough to know what, or what I should do.

    I’d appreciate some guidance.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The problem is that the original in archive.php was for archives, and you are trying to put single post page navigation links in there. Note the different between get_previous_post_link and get_previous_posts_link is the plural of post. The plural is for archives and the single is for single. If you are on an archive page, you are already going to see the same category on the next page. It’s when you are on single.php that you need to put the same_term parameter.
    You would be better off with
    <?php the_post_navigation( array( 'in_same_term' => true, taxonomy' => 'category' )); ?>
    but it won’t be in the same HTML structure, so your theme’s styles wouldn’t work with it.

    So, scratch what you had in archive.php, and start changing single.php instead. Don’t put the variables in the function call; just put the values.

    https://developer.wordpress.org/reference/functions/get_previous_post_link/

    Thank you so much. I see part of what my error was(wrong template).

    I’ve now looked at single.php and have copied the following code:

    <?php if( get_previous_post_link() || get_next_post_link() ) : ?>
    						<ul class="pager">
    						<?php if( get_previous_post_link() ) : ?>
    							<li class="previous"><?php previous_post_link( '%link', __( '&larr; Previous', 'latte' ) ); ?></li>
    						<?php endif; ?>
    						<?php if( get_next_post_link() ) : ?>
    							<li class="next"><?php next_post_link( '%link', __( 'Next &rarr;', 'latte' ) ); ?></li>
    						<?php endif; ?>
    						</ul>
    					<?php endif; ?>

    I see that this is a generic get the previous/next link.

    The only changes to code I’ve made up to now are puttins inline styles where my stylesheet changes don’t seem to work, so I’m not sure what you mean by

    Don’t put the variables in the function call; just put the values.

    Forget for a moment that I put the code in the wrong template, what should I change to get the result that I’m looking for? I really appreciate the help.

    Try this:

    <?php 
    $prev = get_previous_post_link( $format, $link, true );
    $next = get_next_post_link( $format, $link, true );
    if( $prev || $next ) : ?>
    	<ul class="pager">
    		<?php if( $prev ) : ?>
    			<li class="previous"><?php echo $prev; ?></li>
    		<?php endif; ?>
    		<?php if( $next ) : ?>
    			<li class="next"><?php echo $next; ?></li>
    		<?php endif; ?>
    	</ul>
    <?php endif; ?>

    Joy, Thank you for having patience with me. I replaced the code in single.php with the above code and, at least, I didn’t get a “fatal error” message. In fact, I got no error at all.

    However the previous/next links did not show up on the pages. So, if I’m following what’s supposed to happen, either “get” isn’t getting the link or the echo isn’t working for some reason.

    Hopefully another tweak will do it?

    I’m sorry, I used the variables that were in the archive.php. Apparently, they aren’t in single.php.

    <?php
    $prev = get_previous_post_link( '&larr; %link', '%title', true );
    $next = get_next_post_link( '%link &rarr;', '%title', true );
    if( $prev || $next ) : ?>
    	<ul class="pager">
    		<?php if( $prev ) : ?>
    			<li class="previous"><?php echo $prev; ?></li>
    		<?php endif; ?>
    		<?php if( $next ) : ?>
    			<li class="next"><?php echo $next; ?></li>
    		<?php endif; ?>
    	</ul>
    <?php endif; ?>

    This uses the title of the post as the link, like the default is. You can change that to Next and Previous though, if you prefer. This also puts the arrows outside the link, like the default, whereas the theme had the arrows as part of the link text.

    Wow, thank you! One final thing and I will be in heaven. I have a tendency to write long titles, so I would like to go with Previous and Next as the link. Do I just take out ‘%title’ or do I replace it with ‘Previous’ and ‘Next’ or something else?

    Yes, just replace %title with the appropriate Next or Previous.

    Joy,

    Many, many thanks for your help and your patience walking me through this. I now have exactly what I wanted. And I learned a lot.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Using categories as if they were separate blogs’ is closed to new replies.