Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Does anyone know how I can set a limit on the char length for posts when using the "content=yes" function?

I need something like excerpt_size but for the content, not excerpt. Before someone suggests using excerpt, it strips out the formatting and line breaks so I want to use the content output function but set a limit like you can with excerpt_size. I hope that makes sense to you.

I don't mind hacking the php in the plugin if someone knows what I need to change in there.

Thanks in advance!

share|improve this question

3 Answers

You could enable links, images and other tags for use with the_excerpt. This might be the most simple solution, but it will apply to all excerpts:

<?php
function my_excerpt_custom($text) {
$raw_excerpt = $text;
if ( '' == $text ) {
    $text = get_the_content('');
    $text = strip_shortcodes( $text ); 
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]&gt;', $text);
    $allowed_tags = '<a>,<p>,<img>';
    $text = strip_tags($text, $allowed_tags);

    $excerpt_word_count = 88; /*** MODIFY THIS. change the excerpt word count to any integer you like.***/
    $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);

    // $excerpt_end = '[...]'; /*** MODIFY THIS. change the excerpt endind to something else.***/
    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $excerpt_length ) {
        array_pop($words);
        $text = implode(' ', $words);
        $text = $text . $excerpt_more;
    } else {
        $text = implode(' ', $words);
    }
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'my_excerpt_custom');
?>

I've added a few of the most likely tags from the code found here: bac consulting website

share|improve this answer
Thanks for the code, I'll try that out. Would this be added to one of the plugin files or to the functions.php file for the theme? – Scoobie Feb 10 '12 at 1:09
I'd add it to my functions.php file, but it might cause a conflict with one of your plug-ins. If it does dis-able the plug-in's one at a time to find the culprit. – Jeremy Jared Feb 10 '12 at 1:17
Thanks for the help, Jeremy! – Scoobie Feb 10 '12 at 1:20
You're welcome. One more thing, if you use the code above you should notice I commented out the line that starts $excerpt_end. I did this because the theme I was testing it on already had a function for this. If your excerpts don't have and ending you can delete the // to enable it. – Jeremy Jared Feb 10 '12 at 1:26
Ah okay, I'll take a look at that when I run it tomorrow. Fingers crossed it wont take much to get it playing nice with everything else. It'll be nice when I can make my own themes and know how all these things work. By the way, is there a "Thank you" button or something on these forums? – Scoobie Feb 10 '12 at 4:02
show 1 more comment

You cannot just cut off the content on any position: The break may occur inside of an element like table, blockquote or pre. The only way to do this per script: use a DOM parser, walk through the parsed document tree, count the characters in each element and stop if you are in a safe context. Very difficult.

The only safe way is to use the_content() and a manually set <!--more--> marker.

share|improve this answer
Thanks for the reply. Is the plugin allows you to set the char count for the excerpt, why can't you do that when using the "content=yes" setting? – Scoobie Feb 10 '12 at 1:08
Because content contains markup. You need to count just the visible characters. – toscho Feb 10 '12 at 2:40
Ah okay, thanks for that :) – Scoobie Feb 10 '12 at 4:00

Go to wpcontent> plugins> list-category-posts> list_cat_posts.php

edit excerpt size to whatever.

                        'id' => '0',
                        'name' => '',
                        'orderby' => 'date',
                        'order' => 'desc',
                        'numberposts' => '50',
                        'date' => 'no',
                        'date_tag' => '',
                        'date_class' =>'',
                        'dateformat' => get_option('date_format'),
                        'author' => 'no',
                        'author_tag' =>'',
                        'author_class' => '',
                        'template' => 'default',
                        'excerpt' => 'yes',
                        **'excerpt_size' => '100',**
                        'excerpt_tag' =>'yes',
                        'excerpt_class' =>'',
                        'exclude' => '0',
                        'excludeposts' => '0',
                        'offset' => '2',
                        'tags' => '',
                        'content' => 'no',
                        'content_tag' => 'yes',
                        'content_class' => '',
                        'catlink' => 'no',
                        'catlink_tag' =>'',
                        'catlink_class' => '',
                        'comments' => 'no',
                        'comments_tag' => '',
                        'comments_class' => '',
                        'thumbnail' => '',
                        'thumbnail_size' => 'thumbnail',
                        'thumbnail_class' => '',
                        'title_tag' => '',
                        'title_class' => '',
                        'post_type' => '',
                        'post_parent' => 'no',
                        'class' => 'lcp_catlist',
                        'customfield_name' => '',
                        'customfield_value' =>'',
                        'customfield_display' =>'',
                        'taxonomy' => '',
                        'categorypage' => '',
                        'morelink' => '',
                        'morelink_class' => ''
share|improve this answer

protected by Community Jan 15 at 12:53

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.