Welcome to Alledia, the #1 Joomla Training Company

Would you like to learn Joomla? We're here to help you: Sign Up Today

  • Professionally-produced training videos and tutorials.
  • Guaranteed same-day answers to all your Joomla questions.
  • No Joomla experience needed. We've taught 1000s of Joomla students.

Home / Joomla Tips & Tricks / Read More on Your Joomla Frontpage 
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.


Free Weekly Joomla Tutorials

Your Comments (11)

CoryWebb
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.

0
Sean Cook
December 11, 2007

Barrie North had some discussion about this as well:
http://www.compassdesigns.net/...-site.html

A lot less hacking involved. smilies/wink.gif

steve
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.

0
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

0
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?

0
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

0
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

0
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

0
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?

0
Andreaz
November 18, 2008

http://extensions.joomla.org/c...Itemid,35/ Well this extension makes read more automatically and it can resize images to your wanted size on fronted. Check it out.

0
baggi
October 27, 2009

confuse with codessmilies/sad.gif

Write comment

 
  smaller | bigger
 

busy
 
Author of this article: Tom Maiaroto

Read more articles by this author: