How to set character limit on the_content() and the_excerpt() in wordpress

后端 未结 9 1329
臣服心动
臣服心动 2020-12-07 23:36

How do I set a character limit on the_content() and the_excerpt() in wordpress? I have only found solutions for the word limit - I want to be able to set an exact amount cha

相关标签:
9条回答
  • 2020-12-07 23:38

    You could use a Wordpress filter callback function. In your theme's directory, create a file called functions.php and add the following in:

    <?php   
      add_filter("the_content", "plugin_myContentFilter");
    
      function plugin_myContentFilter($content)
      {
        // Take the existing content and return a subset of it
        return substr($content, 0, 300);
      }
    ?>
    

    The plugin_myContentFilter() function will be called each time you request the content of a post/page via the_content() - it provides you with the content as an input, and will use whatever you return from the function for subsequent output or other filter functions.

    You can also do the same for the_exercpt() - add_filter() and then a function to be used as a callback.

    See the Wordpress filter reference docs for more details.

    0 讨论(0)
  • 2020-12-07 23:47

    Or even easier and without the need to create a filter: use PHP's mb_strimwidth to truncate a string to a certain width (length). Just make sure you use one of the get_ syntaxes. For example with the content:

    <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '...');?>
    

    This will cut the string at 400 characters and close it with .... Just add a "read more"-link to the end by pointing to the permalink with get_permalink().

    <a href="<?php the_permalink() ?>">Read more </a>
    

    Of course you could also build the read more in the first line. Than just replace '...' with '<a href="' . get_permalink() . '">[Read more]</a>'

    0 讨论(0)
  • 2020-12-07 23:48

    wp_trim_words() This function trims text to a certain number of words and returns the trimmed text.

    $excerpt = wp_trim_words( get_the_content(), 40, '<a href="'.get_the_permalink().'">More Link</a>');
    

    Get truncated string with specified width using mb_strimwidth() php function.

    $excerpt = mb_strimwidth( strip_tags(get_the_content()), 0, 100, '...' );
    

    Using add_filter() method of WordPress on the_content filter hook.

    add_filter( "the_content", "limit_content_chr" );
    function limit_content_chr( $content ){
        if ( 'post' == get_post_type() ) {
            return mb_strimwidth( strip_tags($content), 0, 100, '...' );
        } else {
            return $content;
        }
    }
    

    Using custom php function to limit content characters.

    function limit_content_chr( $content, $limit=100 ) {
        return mb_strimwidth( strip_tags($content), 0, $limit, '...' );
    }
    
    // using above function in template tags
    echo limit_content_chr( get_the_content(), 50 );
    
    0 讨论(0)
  • 2020-12-07 23:55

    wp_trim_words This function trims text to a certain number of words and returns the trimmed text.

    Example:-

    echo wp_trim_words( get_the_content(), 40, '...' );
    
    0 讨论(0)
  • 2020-12-07 23:55

    For Using the_content() functions (for displaying the main content of the page)

    $content = get_the_content();
    
    echo substr($content, 0, 100);
    

    For Using the_excerpt() functions (for displaying the excerpt-short content of the page)

    $excerpt= get_the_excerpt();
    
    echo substr($excerpt, 0, 100);
    
    0 讨论(0)
  • 2020-12-07 23:56
    <?php 
    echo apply_filters( 'woocommerce_short_description', substr($post->post_excerpt, 0, 500) ) 
    ?>
    
    0 讨论(0)
提交回复
热议问题