Pike800
Pike800

Reputation: 61

Modifying a blog page to only display posts from same category when next/prev link is pressed

I've seen a very similar post to this and lots of other examples such as the wordpress documentation for custom functions here: Function reference for next/prev post link

However, my example sits within what appears to be a custom function within the Storefront (woothemes) theme and so I'm having trouble incorporating the "$in_same_term = true," in the right way without breaking the functionality. Here is the (raw) bit of code in the 'post.php' file within the theme where I think I need to somehow incorporate the "$in_same_term = true,"...

    if ( ! function_exists( 'storefront_paging_nav' ) ) {
/**
 * Display navigation to next/previous set of posts when applicable.
 */
function storefront_paging_nav() {
    global $wp_query;
        $args = array(
        'type'      => 'list',
        'next_text' => _x( 'Next', 'Next post', 'storefront' ) . '&nbsp;<span class="meta-nav">&rarr;</span>',
        'prev_text' => '<span class="meta-nav">&larr;</span>&nbsp' . _x( 'Previous', 'Previous post', 'storefront' ),
        );

    the_posts_pagination( $args );
}
    if ( ! function_exists( 'storefront_post_nav' ) ) {
/**
 * Display navigation to next/previous post when applicable.
 */

function storefront_post_nav() {

    $args = array(
        'next_text' => '%title &nbsp;<span class="meta-nav">&rarr;</span>',
        'prev_text' => '<span class="meta-nav">&larr;</span>&nbsp;%title',
        );
    the_post_navigation( $args );
}
    }

I'm thinking I could be close as loads of searches have revealed the same kind of info, I'm just not integrating it properly...

Thanks in advance for any suggestions on how best to incorporate this functionality!

Upvotes: 0

Views: 712

Answers (2)

botiq
botiq

Reputation: 11

You have just to add 'in_same_term' => true in the array of the storefont_post_nav function

function storefront_post_nav() {
    $args = array(
        'next_text' => '%title &nbsp;<span class="meta-nav">&rarr;</span>',
        'prev_text' => '<span class="meta-nav">&larr;</span>&nbsp;%title',
        'in_same_term' => true,   
    );
}

Is possible to add other options in the array. You can have a look here https://developer.wordpress.org/reference/functions/the_post_navigation/

Upvotes: 1

Pike800
Pike800

Reputation: 61

Ok, so my main problem here was trying to incorporate the code in the wrong place - all I had to do was change the next_post_link parameter in this file: wp-includes/link-template.php. (Not the post.php file as I was trying to do).

It's worth noting for each next and previous link there were 2 sets of parameters where $in_same_term = false needed to be changed to true.

Works a treat now!

Upvotes: 0

Related Questions