Search code examples
magentoblogstruncate

How to Truncate a post using PHP?


PROBLEM: Need to Truncate the post.

CURRENT SITUATION: Whole post is showing. [Read More...] in wrong position.

GOAL: Truncate to 350 characters of the post showing. Place "[Read More...]" where end of text is.

Please see current code:

<?php if ($posts = $this->getRecent()):

// get The ReadMore value from configuration
$configReadmore  = Mage::getStoreConfig('blog/blog/readmore',Mage::app()->getStore()); 

?>
<div class="widget-latest">
    <center><font size=6em><><?php echo Mage::helper('blog')->__('Top Articles'); ?></font size></font color></center>
    <ul style="list-style: none; padding-left: 0px;">
        <?php foreach ($posts as $post): ?>
        <li style="border: 1px solid ; margin-bottom: 10px;">
            <div class="postTitle">
                <h2><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h2>

            </div>

         <?php echo $post->getShortContent(); ?>
                <br/><br/><a href="' . $post->getAddress() . '">[Read More...]</a>


            </li>
        <?php endforeach; ?>
    </ul>
    </div>

Bottom of Website:


Solution

  • Use this code for no of character display:

    <?php Mage::helper('core/string')->truncate($post->getShortContent(), 100);  ?>
    

    More info to code