简体   繁体   中英

WordPress Previous and Next in only one Category - posts with multiple Categories

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 (eg 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 (eg portfolio-1 )?

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

Thanks for help.

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"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM