WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need help editing function (9 posts)

  1. dubbinz
    Member
    Posted 1 year ago #

    I have a code snippet which I placed in my functions.php The function adds next/previous to posts using shortcodes

    here is the code

    add_shortcode( 'prev', 'prev_shortcode' );
    add_shortcode( 'next', 'next_shortcode' );
    function next_shortcode($atts) {
        global $post;
        ob_start();
        next_post_link( '<div class="nav-next">%link</div>', 'Next post link' );
        $result = ob_get_contents();
        ob_end_clean();
        return $result;
    }
    
    function prev_shortcode($atts) {
        global $post;
        ob_start();
        previous_post_link( '<div class="nav-previous">%link</div>', 'Previous post link' );
        $result = ob_get_contents();
        ob_end_clean();
        return $result;
    }

    What I want to do is edit this code so it only cycles through one category in date order which I assume is default anyway. I am limited in my ability with php so I need help to accomplish what I want. can anyone help?

  2. Shaun Scovil
    Member
    Posted 1 year ago #

    Here you go:

    add_shortcode( 'prev', 'prev_shortcode' );
    add_shortcode( 'next', 'next_shortcode' );
    function next_shortcode($atts) {
        global $post;
        ob_start();
        next_post_link( '<div class="nav-next">%link</div>', 'Next post link', true );
        return ob_get_clean();
    }
    
    function prev_shortcode($atts) {
        global $post;
        ob_start();
        previous_post_link( '<div class="nav-previous">%link</div>', 'Previous post link', true );
        return ob_get_clean();
    }
  3. Shaun Scovil
    Member
    Posted 1 year ago #

    There is an optional third parameter in next_post_link() and previous_post_link() to indicate whether the next/prev post must be within the same category as the current post. Default is false.

  4. dubbinz
    Member
    Posted 1 year ago #

    @Shaun Scovil
    like I said I have no idea how to edit it so I completely confused by the response. I know that the changes I need have to be in the () but I have no idea what to put or how to edit it.

    Can you give me an example of the edited version of the code snippet so i can understand?

  5. Shaun Scovil
    Member
    Posted 1 year ago #

    Just replace your old code (the whole thing from your first post) with my code (the whole thing from my response).

  6. Shaun Scovil
    Member
    Posted 1 year ago #

    Specifically, I changed this:

    next_post_link( '<div class="nav-next">%link</div>', 'Next post link' );

    ...to this:

    next_post_link( '<div class="nav-next">%link</div>', 'Next post link', true );

    ...for both the next and previous post functions.

    I also cleaned up the code a bit, using ob_get_clean() instead of all that $result business. But that was just an added bonus. :-)

  7. dubbinz
    Member
    Posted 1 year ago #

    @Shaun Scovil sorry shaun but when I responded I only saw your 2nd post, for whatever reason I couldn't see your first response so I was immediately confused. thanks for the help.

  8. dubbinz
    Member
    Posted 1 year ago #

    This is now resolved

  9. Shaun Scovil
    Member
    Posted 1 year ago #

    Glad I could help!

Topic Closed

This topic has been closed to new replies.

About this Topic