• I am using the theme “Neubau” from elmastudio.com. My problem: I have three categories for my posts: portfolio-1, portfolio-2, portfolio-3. To show only some of my posts on the frontpage I add a fourth category: home.

    [ Moderator note: moved to Fixing WordPress. Please do not use Developing With WordPress for these topics. ]

    The menue of the website has sub items to show only posts of one category: portfolio-1, portfolio-2, portfolio-3. As long as the posts have only one category the next/previous post will be inside that category. For this function I used the code snippet:

    'in_same_term' => 'true'

    If the post has two categories – e.g. portfolio-1 and home – the next or previous post link leads not to a portfolio-1 post, it leads to a post with the category home (portfolio-2 and home or portfolio-3 and home).

    How can I code it so that the next/previous post will not be one of the category home? Or is there any plugin for that (which I didn′t fond)? I found some websites with similiar problems, but I could not transfer the solutions to my code.

    This is the code in “link-template.php”:

    function get_the_post_navigation( $args = array() ) {
        $args = wp_parse_args( $args, array(
            'prev_text'          => '%title',
            'next_text'          => '%title',
            'in_same_term'       => false,
            'excluded_terms'     => '',
            'taxonomy'           => 'category',
            'screen_reader_text' => __( 'Post navigation' ),
        ) );
    
        $navigation = '';
    
        $previous = get_previous_post_link(
            '<div class="nav-previous">%link</div>',
            $args['prev_text'],
            $args['in_same_term'],
            $args['excluded_terms'],
            $args['taxonomy']
        );
    
        $next = get_next_post_link(
            '<div class="nav-next">%link</div>',
            $args['next_text'],
            $args['in_same_term'],
            $args['excluded_terms'],
            $args['taxonomy']
        );
    
        // Only add markup if there's somewhere to navigate to.
        if ( $previous || $next ) {
            $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
        }
    
        return $navigation;
    }

    And this is the code in “single.php” in the child-theme (here I added the code snippet ‘in_same_term’ => ‘true’:

    
    <?php the_post_navigation( array (
                'next_text' => '<span class="meta-nav">' . __( 'Previous', 'neubau' ) . '</span> ' .
                    '<span class="screen-reader-text">' . __( 'Previous Post', 'neubau' ) . '</span> ',
                'prev_text' => '<span class="meta-nav">' . __( 'Next', 'neubau' ) . '</span> ' .
                    '<span class="screen-reader-text">' . __( 'Next Post', 'neubau' ) . '</span> ',
                'in_same_term' => 'true',
            ) ); ?>

    Thanks for help or advices.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Add the ‘home’ category to ‘excluded_terms’ parameter in get_the_post_navigation() function.

    Thread Starter orangeletter

    (@orangeletter)

    I did it. Result: Inside the category e.g. portfolio-1 the post with two categories – home and portfolio-1 – was excluded from next/previous.

    I am astonished that WordPress does not recognize that e.g. portfolio-1 is the only category for next/previous. Why does it jump to home? Is there any category-preference or hierachy WordPress is following?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Previous / Next in only one Category – posts with multiple Categories’ is closed to new replies.