Dec 10 2007
Read More on Your Joomla Frontpage Print
Monday, 10 December 2007

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.


Signup for the monthly Alledia newsletter with great Joomla SEO advice:        
Comments (9)Add Comment
Plugin?
written by Cory Webb, December 10, 2007
Wouldn't it be possible to handle this with a plugin? I hesitate to modify core files unless it is absolutely necessary.
Little easier method...
written by Sean Cook, December 10, 2007
Barrie North had some discussion about this as well:
http://www.compassdesigns.net/...-site.html

A lot less hacking involved. smilies/wink.gif
...
written by Steve Burge, December 11, 2007
Hi Sean

Original credit goes to Hummerbie on this one smilies/smiley.gif

Their hacks are simply changing the text. This is a wholesale change to the code to give you control over how many characters appear before the "read more" kicks in.
...
written by JL, December 19, 2007
Hi, I have been trying to do this but the modifications let me all the front page articles without any text, If I choose -> "Hide intro text" it shows me the introtext with the new parameter (50) but it's a lot of work doing this with each article you know?

Any help? Please smilies/sad.gif

P.D: sorry for my bad English smilies/sad.gif
...
written by TomM, December 21, 2007
you want to change the default 50 to something else?
it's in the xml file.

then you don't have to go to each article and change 50 to whatever.

is this what you were after?
error?
written by Kim Knudsen, January 25, 2008
Hi, when trying to modify my website with the above, all my frontpage articles shows up with the following text "you are not authorized to view this ..." or something like that. PLZ. HELP

nimmer
...
written by Kim Knudsen, January 27, 2008
Ok, I managed to fix my first error - I upgraded my joomla to 1.0.13 - I was running 1.0.11. But now I have the same problem as JL above. All my articles shows up with no content on the frontpage. I have to change every article to not show the introtext before it actually shows up. Plz help

nimmer
...
written by Muhammad Noman, March 19, 2008
hi i want "ReadMore" link dynamically i.e in some pages it will appear as "Read More..." and in some pages as "View Details...". How can i do this? any idea? plz help
...
written by Joao Goncalves, May 23, 2008
I liked the layout of this website.
How can i implement the "Read More" & "Add Comment (x)" like this site has? (The pics too.)

How do can do this?
Any tutorials/text?

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 
right