WordPress.org

Ready to get started?Download WordPress

Forums

[How To] Limit Characters in content (3 posts)

  1. westondeboer
    Member
    Posted 5 years ago #

    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'); ?>

  2. westondeboer
    Member
    Posted 5 years ago #

  3. Huroman
    Member
    Posted 4 years ago #

    Works like charm! Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.