Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1357974
    tey9170
    Participant

    Hi! I’m currently running WordPress 4.7.2, Version 4.6.4 of X Theme. http://froglation.xyz

    Is there a way to limit the posts navigation to the posts in each category?

    I currently have this code added to my function.php file.

    add_action ('the_content', 'add_next_post_item_link' );
    
    function add_next_post_item_link ( $content ) {
    	if ( is_singular('post') ) {
    		$next_name = "<- Previous Chapter <i class=\"x-icon-chevron-right\"></i>";
    		$prev_name = "<i class=\"x-icon-chevron-left\"></i> Next Chapter -> ";
    		$next_link = get_next_post() ? '<a style="float: right;" href="' . get_permalink( get_next_post()->ID ) . '">' . $prev_name . '</a>' : '';
    		$prev_link = get_previous_post() ? '<a style="float: left;" href="' . get_permalink( get_previous_post()->ID ) . '">' . $next_name . '</a>' : '';
    		$posts = '<div class="wp-post-navigation-next">'.$next_link.$prev_link.'</div>';
    		return  $posts . $content . $posts;
    	} else {
    		return $content;
    	}
    }

    Please advice on the modifications required.

    Thanks!

    #1358262
    Rue Nel
    Moderator

    Hello There,

    Thanks for writing in! You can update it and make use of this code:

    add_action ('the_content', 'add_next_post_item_link' );
    
    function add_next_post_item_link ( $content ) {
    	if ( is_singular('post') ) {
    		$next_name = "<- Previous Chapter <i class=\"x-icon-chevron-right\"></i>";
    		$prev_name = "<i class=\"x-icon-chevron-left\"></i> Next Chapter -> ";
    		$next_link = get_next_post(true, '', 'category') ? '<a style="float: right;" href="' . get_permalink( get_next_post()->ID ) . '">' . $prev_name . '</a>' : '';
    		$prev_link = get_previous_post(true, '', 'category') ? '<a style="float: left;" href="' . get_permalink( get_previous_post()->ID ) . '">' . $next_name . '</a>' : '';
    		$posts = '<div class="wp-post-navigation-next">'.$next_link.$prev_link.'</div>';
    		return  $posts . $content . $posts;
    	} else {
    		return $content;
    	}
    }

    Please let us know if this works out for you.

    #1358419
    tey9170
    Participant

    That totally solves the problem!
    Thank you so much!

    Keep up the good work X

    #1358478
    Thai
    Moderator

    You’re most welcome 🙂

    If you need anything else please let us know.

  • <script> jQuery(function($){ $("#no-reply-1357974 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>