Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Limit Character Count in the_content; keep MORE link (1 post) WordPress

  • SOLVED

reference article in WP.org forum: [[LINK href="http://wordpress.org/support/topic/how-to-limit-characters-in-content?replies=3"]]http://wordpress.org/support/topic/how-to-limit-characters-in-content?replies=3[[/LINK]]

I've managed to limite my character count on posts by using the code listed in that forum discussion. however, i love my "more" link when i use that code.

how can i limit the character count in the_content and continue to use the MORE link?

this is the code for limiting the character count:

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;
}



development website:
http://sandbox.bucktowndigital.com/buzz/

Answers (2)

2011-02-03

Oleg Butuzov answers:

get more link is generated by the get_the_permalink() function...

so... echo $content.'<a href="'.get_the_permalink().'">more..</a>';


sergi comments:

hi, oleg,

i had to write it as below, or it i'd get an error message for that line.

echo $content.'<a href=".get_the_permalink()">more...</a>';

see how i had to remove the single apostrophes surrounding the .get_the_permalink()?

it looks good, but the MORE link returns this url: http://sandbox.bucktowndigital.com/buzz/page/2/.get_the_permalink%28%29

what should i do?

thanks
sergi


Oleg Butuzov comments:

parser perhaps kill part of string...

echo $content.'<a href=".get_the_permalink().">more...</a>';


Oleg Butuzov comments:

echo $content;
echo '<a href="'.get_the_permalink().'">more..</a>';

corrected previous code.

sorry....


sergi comments:

hi, oleg

it seemed to make it worse. see error: http://sandbox.bucktowndigital.com/buzz/page/2/

thanks

2011-02-03

Sébastien | French WordpressDesigner answers:

replace your function
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;
}

by

function content($num) {
$theContent = get_the_content();
$link=get_the_permalink();
$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)."... <a href='".$link."'>more..</a>";
echo $content;
}