Fields in page.tpl.php

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.

– Answer –

  • 18 March 2011: Answer by Aaron for Fields in page.tpl.php -

    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

  • 18 March 2011: Answer by David Lanier for Fields in page.tpl.php -

    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
  • 18 March 2011: Fields in page.tpl.php -

    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.

Leave a Reply