1

I want to display in my page.tpl.php 3 custom fields form the latest content type added to my website.

I have a custom content type called "Articles" with three custom fields, field_1, field_2, field_3.

How can I display on every page of my website those three fields from the latest content added to "articles"? I want to do this without using views.

4
  • 5
    Is this a exercise in ideology? Views is very well suited to this task, why not use it? Mar 18 '11 at 2:10
  • 1
    Are you interested in an answer for Drupal 6, or Drupal 7?
    – apaderno
    Mar 18 '11 at 7:23
  • Hi, I already built this with views but I used views only for this small feature on the website and I was curious if there is an simple alternative.
    – Sergiu B
    Mar 18 '11 at 9:31
  • I used Drupal 7
    – Sergiu B
    Mar 18 '11 at 9:32
1

I don't have the exact answers for you, but I do have some directions to point you. Try something like this:

  1. In template.php, in a THEMENAME_preprocess_page() function, construct a query to get the node ID of the most recently created node. Look up Drupal's Database API to see how to do this. (A more permanent solution would be to use a custom module for the query, but let's start at the theme layer to get this working.)
  2. Do a node_load() to get the entire contents of that node, which will contain your three fields, and more.
  3. Create variables (inside the $variables[] array) to hold the rendered content of your custom fields.
  4. Use those new variables in page.tpl.php, or page-article.tpl.php
2
  • It is not necessary to load a node inside a page preprocess function; if $vars is the argument passed to the function, then $vars['node'] already contains the node object, when the page being preprocessed is a node page.
    – apaderno
    Mar 18 '11 at 7:26
  • @kiamlaluno it will only be there if you are on a node page, the question is about placing node content on every page. Mar 18 '11 at 9:44
1

Since I'm going to assume you have some kind of religious opposition to the Views module- I would opt for the custom module method.

A simple module could create a block that can be displayed on any page you like.

I would start here, http://drupal.org/node/206753. We're talking 6.x right? Of course we are. Once you have the .info and .module file setup, you can skip down to Declaring Block Content, (http://drupal.org/node/206758), and then onto Generating content (http://drupal.org/node/206759 )... this is where it gets interesting...

function yourModuleName_block($op='list', $delta=0) {
  // set up the block  
  $block = array();

  if ($op == "list") {
    // This is what it will be listed as in your list of blocks
    $block[0]["info"] = t('On This Date');
  } 
  else if ($op == 'view') {
     $block['content'] = yourfunctionToGetNewArticalContent();
  }
}

Then create your function to return anything you want, For that you''ll want to take a look over here.. http://api.drupal.org/api/drupal/includes--database.mysql-common.inc/function/db_query/6

1
  • It wasn't tagged as Drupal 7 when I answered and Drupal 6 is still far more common.
    – Aaron
    Mar 20 '11 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.