Joomla! Mod Latest News with Intro Text

How to Modify Latest News

When you include a Latest news module on your site, it lists some number of article titles as list item links in a ul for you.

What if you also wanted to see a little bit of the article’s content with the link? Simple, make a copy of the mod_latestnews module and modify it just a little bit.

 

Looking inside mod_latestnews/helper.php, you can find where the list of items is returned to the module to display. The query already gets all of an article’s content, but the list returned only contains a title.

 

         $i        = 0;

        $lists    = array();

        foreach ( $rows as $row )

        {

            $lists[$i]->link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug, $row->sectionid));

            $lists[$i]->text = htmlspecialchars( $row->title );

            $i++;

        }

 

We just need to grab the article content also. Article content has two parts, really. The intro text and the full text. So we’re going to get both. We’ll use the intro if it’s there and the full if it isn’t, later on when we build the ul.

         $i        = 0;

        $lists    = array();

        foreach ( $rows as $row )

        {

            $lists[$i]->link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug, $row->sectionid));

            $lists[$i]->text = htmlspecialchars( $row->title );

            $intro = htmlspecialchars( $row->introtext );
            //probably should strip images out or something here
            $lists[$i]->intro = $intro;
            $full = htmlspecialchars( $row->fulltext );
            //probably should strip images out or something here
            $lists[$i]->full = $full;

            $i++;

        }

 

Now, let’s look at the  mod_latestnews/tmpl/default.php which takes this list we just built and displays it;

 <ul class=”latestnews<?php echo $params->get(‘moduleclass_sfx’); ?>”>

<?php foreach ($list as $item) :  ?>

    <li class=”latestnews<?php echo $params->get(‘moduleclass_sfx’); ?>”>

        <a href=”<?php echo $item->link; ?>” class=”latestnews<?php echo $params->get(‘moduleclass_sfx’); ?>”>

            <?php echo $item->text; ?></a>

    </li>

<?php endforeach; ?>

</ul>

 We want to add some of the article’s text. Check if there is intro text and use that if there is; otherwise, use fulltext.

<ul class=”latestnews<?php echo $params->get(‘moduleclass_sfx’); ?>”>

<?php foreach ($list as $item) :  ?>

    <li class=”latestnews<?php echo $params->get(‘moduleclass_sfx’); ?>”>

        <a href=”<?php echo $item->link; ?>” class=”latestnews<?php echo $params->get(‘moduleclass_sfx’); ?>”>

            <?php echo $item->text; ?></a>

            <br  />

            <?php

                if (!empty($item->intro)) {

                    echo substr($item->intro, 0, $params->get(‘introlength’));

                } else {

                    echo substr($item->full, 0, $params->get(‘introlength’));

                }

                ?>

    </li>

<?php endforeach; ?>

</ul>

Log in to download the completed module.

Leave a Reply

Your email address will not be published. Required fields are marked *