Blog Entries with different lengths of info

3 posts by 2 authors in: Forums > CMS Builder
Last Post: December 6, 2012   (RSS)

By crazyfish - December 6, 2012 - edited: December 6, 2012

A client has a blog, most are short and sweet but every now and then, the content goes on forever - and they add images. This is for a list page.

So, how do I let short ones run, and long ones have a link to a details page?

Logic something like this:

<?php if ($record['content']$maxwords > 25): ?>
show 25 words then link<br/><br/>
<?php endif ?>

else show the entire post and no link

( really it's the first line of the code that has me stumped )
Thanks in advance!

Re: [crazyfish] Blog Entries with different lengths of info

Hi,

I've done some testing, and this should do the trick:

<?php if(str_word_count($record['content']) > 25): ?>
<?php echo implode(' ', array_slice(str_word_count($record['content'], 1),0,25)); ?><br>
<a href="<?php echo $record['_link'] ?>" style="color: #008000"><?php echo $record['_link'] ?></a><br/><br/>
<?php else: ?>
<?php echo $record['content']; ?><br>
<?php endif; ?>


The PHP function str_word_count will give you the number of words in a string. If it's greater than 25 words, then display the first 25 words of the string, using str_word_count function to break the string into an array of words, then using array slice to select the first 25 words, and gluing the array back into a string with the implode function.

If the $record['content'] is a wysiwyg editor field, it might be worth using the php function strip_tags (http://php.net/manual/en/function.strip-tags.php). To remove any images and other tags, as these will be included in the word count, and you could end up with an opening tag being included in the first 25 words, but not a closing tag.

Let me know if you have any questions.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com