问题
I need to customize the previous_post_link() and next_post_link() on WordPress.
Per example, I want to shrink permalinks like "Top 5 programming languages that you need to learn" to "Top 5 programing...".
The function responsible for creating the link isadjacent_post_link() located at wp-includes/link-template.php
回答1:
To create a custom adjacent link for posts I can make use of the filter hook next_post_link and previos_post_link;
In the functions.php:
function shrink_previous_post_link($format, $link){
    $in_same_cat = false;
    $excluded_categories = '';
    $previous = true;
    $link='%title';
    $format='« %link';
    if ( $previous && is_attachment() )
        $post = & get_post($GLOBALS['post']->post_parent);
    else
        $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
    if ( !$post )
        return;
    $title = $post->post_title;
    if ( empty($post->post_title) )
        $title = $previous ? __('Previous Post') : __('Next Post');
    $rel = $previous ? 'prev' : 'next';
    //Save the original title
    $original_title = $title;
    //create short title, if needed
    if (strlen($title)>40){
        $first_part = substr($title, 0, 23);
        $last_part = substr($title, -17);
        $title = $first_part."...".$last_part;
    }   
    $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'" title="'.$original_title.'">';
    $link = str_replace('%title', $title, $link);   
    $link = $string . $link . '</a>';
    $format = str_replace('%link', $link, $format);
    echo $format;   
}
function shrink_next_post_link($format, $link){
    $in_same_cat = false;
    $excluded_categories = '';
    $previous = false;
    $link='%title';
    $format='%link »';
    if ( $previous && is_attachment() )
        $post = & get_post($GLOBALS['post']->post_parent);
    else
        $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
    if ( !$post )
        return;
    $title = $post->post_title;
    if ( empty($post->post_title) )
        $title = $previous ? __('Previous Post') : __('Next Post');
    $rel = $previous ? 'prev' : 'next';
    //Save the original title
    $original_title = $title;
    //create short title, if needed
    if (strlen($title)>40){
        $first_part = substr($title, 0, 23);
        $last_part = substr($title, -17);
        $title = $first_part."...".$last_part;
    }   
    $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'" title="'.$original_title.'">';
    $link = str_replace('%title', $title, $link);   
    $link = $string . $link . '</a>';
    $format = str_replace('%link', $link, $format);
    echo $format;   
}
add_filter('next_post_link', 'shrink_next_post_link',10,2);
add_filter('previous_post_link', 'shrink_previous_post_link',10,2);
That all I needed to do. Thanks!
回答2:
the next_post_link() and previous_post_link() function both come with customization options.  http://codex.wordpress.org/Function_Reference/next_post_link.  After reading that and learning what the acceptable arguments to the function are,  Test to see if it is possible to pass a php function to the option, like substr().  
<?php next_post_link('%link',substr('%title',20),FALSE);?>
'%link' and '%title' are shortcodes to the post link and title.
Let us know if it works out.
来源:https://stackoverflow.com/questions/3542739/how-to-customize-wordpress-internal-functions-like-adjacent-post-link