how to customize wordpress internal functions, like adjacent_post_link()

后端 未结 2 1603
臣服心动
臣服心动 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: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='« %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 = '';
        $link = str_replace('%title', $title, $link);   
        $link = $string . $link . '';
    
        $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 = '';
        $link = str_replace('%title', $title, $link);   
        $link = $string . $link . '';
    
        $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!

提交回复
热议问题