Read More on Your Joomla Frontpage

This is a guest post from Tom Maiaroto of ExpandTheRoom. In this post he explains how to set the "Read More" on your Joomla frontpage to appear after a certain number of characters. This is useful to keep a standard appearance for your articles when you have a large number of people submitting content.

This post grew out of comment that Tom made on our original discussion about the "read more" on Joomla’s frontpage.

We welcome other guest posts. Simply login and click "Make a Guest Blog Post" in the right-hand menu.

How to Change the Joomla Code to Control "Read More"

1. The first file to change is components / com_content / content.php.

This is what my content.php looks like: (search for this under frontpage() method)

// query records

$query = "SELECT a.id, a.title, a.title_alias, a.introtext, a.sectionid, a.state, a.catid, a.created, a.created_by, a.created_by_alias, a.modified, a.modified_by,"

and simply add the other column in the database for attribs.

$query = "SELECT a.attribs, a.id, a.title, a.title_alias, a.introtext, a.sectionid, a.state, a.catid, a.created, a.created_by, a.created_by_alias, a.modified, a.modified_by,"


Also, if you want truncated words to appear on blog sections you’ll need to add the same "a.attribs" to the selects there as well.


For example, showBlogSection() method has this line:

// Main data query
    $query = "SELECT a.id, a.title, a.title_alias, a.introtext, a.sectionid, a.state, a.catid, a.created, a.created_by, a.created_by_alias, a.modified, a.modified_by ,"


Simply add in "a.attribs" again to that select query like for the frontpage() method.

2. Now put into your show() method:

$article_params = new mosParameters( $row->attribs ); 

This is a brand new line that lets us use the article’s params since the $params here are menu item params:

$introtext_word_cap = $article_params->get( ‘introtext_count’ ); 

introtext_count is what I called this param, and in the final step of this tutorial we’ll create it in the /administrator/components/com_content/content.xml file

3. Now you’ll add in the logic to truncate where it goes into displaying the introtext.

if ( $params->get( ‘introtext’  ) ) {

       

        //$row->text = $row->introtext. ( $params->get( ‘intro_only’ ) ? ” : chr(13) . chr(13) . $row->fulltext); // <— Here’s the original line.

           

// NEW – truncate stuff

                // $params->_params->menu_image will only exist on an index page…this is the part I’m not 100% happy with, it’s hacky but does its job of determining if we’re on an index/blog/front page or on an article view page



            if(isset($introtext_word_cap) && $params->_params->menu_image) {

                $words = $introtext_word_cap;

                $wordCount = count(explode(" ",$row->introtext));           

                    if ($wordCount<$words) {

                        $adjustedText = $row->introtext;

                    } else {

                          $adjustedText = implode(" ", array_slice(explode(" ",$row->introtext), 0, $words)) . ‘…’;

                      }

            } else {

                  $adjustedText = $row->introtext;

              }

            // end truncate stuff   

           

        $row->text = $adjustedText . ( $params->get( ‘intro_only’ ) ? ” : chr(13) . chr(13) . $row->fulltext);

    } else {

        $row->text = $row->fulltext;

    }

4. Edit the administrator / components / com_content / content.xml file.

An finally, here’s that extra parameter in the content.xml file (note a default of 50 can change to whatever):

<param name="introtext_count" type="text" size="20" default="50" label="Intro Text Word Count" description="How many words to display of the intro text." />

About Tom Maiaroto

My name is Tom Maiaroto and I’m a web developer/designer for ExpandTheRoom located in NYC. My personal blog is at www.concepthue.com/blog. I’ve been working with Joomla! for a few years now as well as other CMS’. I also create my own CMS systems from scratch.

Leave a Reply

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