Support » Fixing WordPress » Need help editing function

  • Resolved dubbinz

    (@dubbinz)


    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?

Viewing 8 replies - 1 through 8 (of 8 total)
  • 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();
    }

    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.

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

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

    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. 🙂

    @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.

    This is now resolved

    Glad I could help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Need help editing function’ is closed to new replies.