2

To click to the next or previous post I use this code

<?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',
        ) ); ?>

'in_same_term' => 'true' is used to open the next or previous post inside the same categoy. But this does not work with multiple categories.

I have three categories for my posts: portfolio-1, portfolio-2, portfolio-3. To show some of these posts on the frontpage I add a fourth category: home. Inside a category (e.g. portfolio-1) I click on the next or previous post link. If the next next or previous post has two categories (portfolio-1 and home) I am redirected to the next post/previous post of home - instead to the next post/previous post of portfolio-1.

How can I remain inside one category (e.g. portfolio-1)?

I found some websites with similiar problems, but I could not transfer te solutions to my code.

Thanks for help.

2

1 Answer 1

0

the file "link-template.php" seems to be the right file for code changes.

function get_the_post_navigation( $args = array() ) {
    $args = wp_parse_args( $args, array(
        'prev_text'          => '%title',
        'next_text'          => '%title',
        'in_same_term'       => true,
        'excluded_terms'     => '7,37',
        '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;
}

This seems to be the important paragraph:

function get_the_post_navigation( $args = array() ) {
    $args = wp_parse_args( $args, array(
        'prev_text'          => '%title',
        'next_text'          => '%title',
        'in_same_term'       => true,
        'excluded_terms'     => '7',
        'taxonomy'           => 'category',
        'screen_reader_text' => __( 'Post navigation' ),
    ) );

I set 'in_same_term' to 'true'. (For testing if "link-template.php" is the right file I deleted the formerly added code 'in_same_term' => 'true' from the file "single.php"). And I added to 'excluded_terms' '7' for the category "home". The result is that previous/next remains inside one category. But the post with two categories - "portfolio-1" and "home" - is not displayed.

Next step: How can I include the post with the two categories "portfolio-1" and "home" and exclude the other posts with the category "home"?

3
  • 2
    Absolutely NOT. Never, ever ever change core WP files. The very next time you run an update, your changes will be lost. Jun 9, 2017 at 13:37
  • I know - better idea?
    – wolfmuc
    Jun 9, 2017 at 15:36
  • ... The better idea is to change the code inside the child-theme´s file "single.php" (see first post of this thread).
    – wolfmuc
    Jun 9, 2017 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.