• August 28, 2017

    https://stackoverflow.com/questions/4082662/multiple-excerpt-lengths-in-wordpress

    1. Limit Post Excerpt Length Using Number Of Words

    functions.php

    function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);
      if (count($excerpt)>=$limit) {
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt).'...';
      } else {
        $excerpt = implode(" ",$excerpt);
      }	
      $excerpt = preg_replace('`[[^]]*]`','',$excerpt);
      return $excerpt;
    }
     
    function content($limit) {
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content).'...';
      } else {
        $content = implode(" ",$content);
      }	
      $content = preg_replace('/[.+]/','', $content);
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]>', $content);
      return $content;
    }

    Now, in every place where you use the_excerpt() or the_content() in your loop, use excerpt($limit) or content($limit).

    For example if you want to limit your excerpt length to 30 words use echo excerpt(30) and for content.

    2. Limiting Excerpt Length To Number of Characters.

    Sometimes you want to limit the post excerpt limit on character and not by words. You just want to limit the character length in excerpt.

    Here is the solution. Add the following code to your function.php file

    function get_excerpt(){
    $excerpt = get_the_content();
    $excerpt = preg_replace(" ([.*?])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, 50);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/s+/', ' ', $excerpt));
    $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
    return $excerpt;
    }

    You can change the length 50 to your desired character length. And also ‘more’ to what text you want to display. Then call your function echo get_excerpt();  wherever you are planning on getting your posts.

    3. Limit Post Summary By Adding read more Tag.

    You can also set your excerpt length by adding more or read more tag using post editor tag option. Simply place the blinking text cursor where you want the excerpt to be stopped and click the ‘more’ tag.

    insert read more tag to limit post excerpt length

    4. Enabling Custom Excerpt To Write Your Own Summary

    Sometimes you might not satisfied with automatically generated excerpt, which might be breaking your site layout.
    There is a feature in WordPress to show an additional field for custom excerpt.

    This feature isn’t activated by default but very simple to enable for any post. Click on Screen Option found above the title in post editor.

    Enabling Custom Excerpts to limit post excerpt length

    By enabling it there is a new field ‘excerpt’ can be seen below the content editor, as you can see in the image above. Now enter your custom excerpt with your desired length to fit your site layout.

    5. Control Excerpt Length Using Filters

    Excerpt length is set to 55 words by default. We can change this default value without overriding default the_excerpt() function, so that you don’t have to make a change in each files and templates where you have used the_excerpt().

    To change this default excerpt length to 20 words using excerpt_length filter, add the following code to functions.php file in your theme:

    function custom_excerpt_length( $length ) {
    	return 20;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

    This way you can limit post excerpts length.

    Hope this article may help you to limit post excerpt length. Comments are welcome.



เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories