Home / Joomla Tips & Tricks / Read More on Your Joomla Frontpage 
Joomla Tips & Tricks
Dec
10
2007
Read More on Your Joomla Frontpage
Written by Tom Maiaroto   
Avatar

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.


 
Author of this article: Tom Maiaroto

Read more articles by this author:

Comments  

 
#1 Cory Webb 2007-12-10 10:20
Wouldn't it be possible to handle this with a plugin? I hesitate to modify core files unless it is absolutely necessary.
Quote
 
 
#2 Sean Cook 2007-12-10 23:14
Barrie North had some discussion about this as well:
www.compassdesigns.net/joomla-blog/joomla-tips/how-to-get-people-to-read-more-of-your-joomla-site.html

A lot less hacking involved. ;-)
Quote
 
 
#3 Steve Burge 2007-12-11 08:59
Hi Sean

Original credit goes to Hummerbie (www.pathos-seo.com/joomlablog/how-to-solve-the-joomla-read-more-problem/) on this one :-)

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.
Quote
 
 
#4 JL 2007-12-19 15:40
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 :sad:

P.D: sorry for my bad English :sad:
Quote
 
 
#5 TomM 2007-12-21 15:26
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?
Quote
 
 
#6 Kim Knudsen 2008-01-25 15:47
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
Quote
 
 
#7 Kim Knudsen 2008-01-27 12:34
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
Quote
 
 
#8 Muhammad Noman 2008-03-19 01:41
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
Quote
 
 
#9 Joao Goncalves 2008-05-23 04:19
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?
Quote
 
 
#10 Andreaz 2008-11-18 06:10
extensions.joomla.org/component/option,com_mtree/task,viewlink/link_id,5738/Itemid,35/ Well this extension makes read more automatically and it can resize images to your wanted size on fronted. Check it out.
Quote
 

Add comment


Security code
Refresh