how to customize wordpress internal functions, like adjacent_post_link()

后端 未结 2 1598
臣服心动
臣服心动 2021-01-26 05:45

I need to customize the previous_post_link() and next_post_link() on WordPress.

Per example, I want to shrink permalinks like \"Top 5 programmi

相关标签:
2条回答
  • 2021-01-26 06:08

    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.

    0 讨论(0)
  • 2021-01-26 06:17

    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='&laquo; %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 &raquo;';
    
        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!

    0 讨论(0)
提交回复
热议问题