WordPress.org

Ready to get started?Download WordPress

Forums

previous_post and next_post OR previous_post_link and next_post_link? (4 posts)

  1. parasew
    Member
    Posted 8 years ago #

    which ones to use? in the codex, previous_post_link is not even mentioned; in the file template-functions-links.php, there is written
    // Deprecated. Use previous_post_link().

    so which one to use now that is safe for the future?
    and how to limit the output-string to a certain number of chars?

  2. skippy
    Member
    Posted 8 years ago #

    previous_post_link() is the preferred function.

    Here's the function declaration:
    function previous_post_link($format='« %link',
    $link='%title',
    $in_same_cat = false,
    $excluded_categories = '')

    So if you want to limit what gets displayed, you'll need to first create the output yourself, then pass it as the $format or $link parameter.

  3. parasew
    Member
    Posted 8 years ago #

    i made a hack to the previous_post() and to the next_post(), maybe this is a nice feature to put in the next release version, as it is fully backwards compatible.

    in template-functions-links.php replace the following two functions
    function previous_post
    function next_post


    function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='', $limit, $more_string) {

    if ( empty($in_same_cat) || 'no' == $in_same_cat )
    $in_same_cat = false;
    else
    $in_same_cat = true;

    $post = get_previous_post($in_same_cat, $excluded_categories);

    if(! $post) {
    return;
    }

    if ((isset($limit)) && ($limit >0)) {
    $s_post=substr($post->post_title,0,$limit);
    if (isset($more_string)) $s_post.=$more_string;
    }
    else {$s_post=$post->post_title;}

    $string = '<a>ID).'">'.$previous;
    if ($title == 'yes') {
    $string .= apply_filters('the_title', $s_post, $post);
    }
    $string .= '</a>';
    $format = str_replace('%', $string, $format);
    echo $format;
    }

    // Deprecated. Use next_post_link().
    function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat='no', $limitnext=1, $excluded_categories='', $limit, $more_string) {

    if ( empty($in_same_cat) || 'no' == $in_same_cat )
    $in_same_cat = false;
    else
    $in_same_cat = true;

    $post = get_next_post($in_same_cat, $excluded_categories);

    if(! $post) {
    return;
    }

    if ((isset($limit)) && ($limit >0)) {
    $s_post=substr($post->post_title,0,$limit);
    if (isset($more_string)) $s_post.=$more_string;
    }
    else {$s_post=$post->post_title;}

    $string = '<a>ID).'">'.$next;
    if ($title=='yes') {
    $string .= apply_filters('the_title', $s_post, $nextpost);
    }
    $string .= '</a>';
    $format = str_replace('%', $string, $format);
    echo $format;
    }

    the functions are called like this:

    previous_post('� %','','yes','','','',8,'(...)');
    next_post(' % �','','yes','','','',9,'(...)');

    the number (7th argument) indicates the length after which the output should be cropped. (optional)
    the 8th argument is the text to put after the cropping.

    useful? i think so!

  4. parasew
    Member
    Posted 8 years ago #

    i posted a more modified version on http://parasew.com/hacks
    the added feature is if the string is shorter than the length argument, it stays untouched.
    have fun!

Topic Closed

This topic has been closed to new replies.

About this Topic