Skip to main content

How to show all of first article on front page

May 6, 2010 by Steve

I want the first article showing on the front page, the most recent one, to show full length, while the rest of the articles show teasers as they normally do. There are not a lot of good references to this in the Drupal.org documentation, nor on the forum, nor elsewhere on the internet. It took a lot more searching around to figure this out than I thought it would.

Basically, there are two ways to do it: use the Views module or hack the theme PHP.

The Views method: Views comes with an example page view that duplicates the front page content. In a discussion on the Drupal.org forum, Drupal user WorldFallz gave me these instructions for modifying that view to show a full node first:

  1. enable the provided frontpage view and clone it (so you can leave the original intact).
  2. edit the 'page' display
  3. edit "items to display" -- set the first number to the number of teasers you want to display, and set the offset to 1 (that's key).
  4. add an 'attachment' display
  5. in 'attachment settings' set 'position' to 'before' and 'attach to' to be your front page page display
  6. edit 'items to display', click the 'override' button, and set number of items to 1 and offset to 0
  7. click the gear next to 'Row style: Node', click the 'override' button, and set build mode to teaser

This works as advertised, though there is one defect. If the node is being added to a book at the time you write it, the book navigation block overrides the links block at the bottom of the article, meaning there is no "Add new comment" or AddToAny link visible.

The PHP hack method: As the page is being built, Drupal uses the node template file to build each node up for display on the page. (This is part of the theme, for my site it is sites/all/themes/newswire/node.tpl.php.) The $node object gets passed to the template for rendering, along with some other information. See the API docs for a full list.

In order to see the structure of the $node object with live data, I installed the Contemplate module and used it to edit a node template. From here I can see all the properties of the $node object and their values with the most recently published node on the site.

The node template has access to two variables we can use to decide when to make the changes to the node layout. $is_front is true if we are on the front page. $id holds the position of the node on the page; the first node on the page has an $id of 1, the second has an $id of 2, and so on. So I was ready to try this in my node.tpl.php file, right before the part where it prints out the $content:

if ($is_front && ($id=="1")):
$body=$node->content['body']['#value'];
$content = check_markup($body);
endif;

It didn't work. Turns out that when the Drupal engine builds the node object, if the object is just a teaser, it only puts the teaser data into $node, not the full body text. So we'll have to use a cheap hack.

In order to get the full text of the node, we have to go all the way back to the database, using the Drupal helper functions to make the query and format the data for display:

if ($is_front && ($id=="1")):
$nid=$node->nid;
$body=db_result(db_query("SELECT body FROM {node_revisions} WHERE nid=%d", $nid));
$content = check_markup($body);
endif;

This works, though it is also not perfect. Because the $links get built up before the template file is called, the links include a "Read More" link, even though we now have the full data available on this node.

Neither solution is perfect, but I prefer the PHP hack to the Views solution myself, because I'd rather have the links block showing than missing.

Still to do: Figure out how to get rid of the 'Read More' link at the bottom of the top article. Actually, I'd like to keep it, but change it to "Permalink" or something like that. I'm going to try the Read More module when I get a chance.

@stevew99999

  • @breathingdakota Ever tried. Ever failed. No matter. Try again. Fail again. Fail better. 5 years 29 weeks ago
  • Beer o'clock @the_nerdery, having some wings and getting set for #btlcap. Yes, #ilovemyjob. 5 years 29 weeks ago
  • @zacknaylor And I want to publish them. Let me know if you need help with that part. 5 years 29 weeks ago

Premium Drupal Themes by Adaptivethemes