WordPress.org

Support

Support » How-To and Troubleshooting » [How To] Limit Characters in content

[How To] Limit Characters in content

  • I wanted to limit the amount of characthers being shown on my custom page, and i wanted to remove blockquotes, [caption] and images also.

    Put this into your functions.php file in your theme directory.

    function content($num) {
    $theContent = get_the_content();
    $output = preg_replace('/<img[^>]+./','', $theContent);
    $output = preg_replace( '/<blockquote>.*<\/blockquote>/', '', $output );
    $output = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $output );
    $limit = $num+1;
    $content = explode(' ', $output, $limit);
    array_pop($content);
    $content = implode(" ",$content)."...";
    echo $content;
    }

    and just echo it out in your theme inside the loop:
    <?php content('20'); ?>

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[How To] Limit Characters in content’ is closed to new replies.
Skip to toolbar