Displaying Article Custom Fields in Layouts

Introduction

Custom fields for an article are defined in the publication.  That is, for each publication you can define the custom fields that will be available for all articles for that publication.

Because custom fields are peculiar to your site, it is difficult to display them in a unified way that will suit everyone.  Therefore, we recommend that the display of custom fields is handled using layout overrides.

Article View

To create a layout override for the article view, copy files to the layout override directory in the default template.  For example, to override the default layout, copy:

/components/com_zine/views/article/tmpl/default.php

to:

/templates/your_template/html/com_zine/article/default.php

then make customizations as required.  For more information about layout overrides, please refer to the Understanding Output Overrides in Joomla! 1.5 tutorial on the Joomla! Developer Site.

To display the custom fields, we make use of the custom variable in the view.  This variable is a JParameter object.

Following are several examples of how to display custom fields:

Display the value of a custom field

<?php echo $this->custom->get( 'field_name' ); ?>

The field_name will come from value of the name attribute you defined in the XML.  For example, if the following code defined your custom field:

<param name="phone"
 type="text"
 class="inputbox"
 label="Phone Number"
 default=""
 description="A contact phone number" />

Then to display this field, use:

<?php echo $this->custom->get( 'phone' ); ?>

Display the value only if set

This code will only display the field if set:

<?php if ($value = $this->custom->get( 'phone' ) : ?>
  <?php echo $value; ?>
<?php endif; ?>

Here is another example using a table:

<table>
  <?php if ($value = $this->custom->get( 'phone' ) : ?>
  <tr>
    <th>
      <?php echo JText::_( 'Phone Number' ); ?>
    </th>
    <td>
      <?php echo $value; ?>
    </td>
  </tr>
  <?php endif; ?>

  <?php if ($value = $this->custom->get( 'mobile' ) : ?>
  <tr>
    <th>
      <?php echo JText::_( 'Mobile Number' ); ?>
    </th>
    <td>
      <?php echo $value; ?>
    </td>
  </tr>
  <?php endif; ?>
</table>

Category View or Modules

Where there is a list of articles such as in the Category View, the Articles View or in many of the modules, displaying custom fields is still easy but requires a little more work.

First create the layout override for the appropriate component view or module.  Then you need to look for the loop that displays the articles.  This can vary from case to case but will look something like:

<?php foreach ($this->articles as $article) : ?>
Some code
<?php endforeach; ?>

or:

<?php foreach ($this->items as $item) : ?>
Some code
<?php endforeach; ?>

The second case is more common.

Now, somewhere within that loop you will need to place the following code:

<?php foreach ($this->items as $item) : ?>
  Probably some code here
  <?php $custom = new JParameter( $item->custom ); ?>
  <?php echo $custom->get( 'phone' ); ?>

  <?php if ($value = $custom->get( 'mobile' ) : ?>
    <?php echo $value; ?>
  <?php endif; ?>
  Probably some code here to
<?php endforeach; ?>

Each time the loop clicks around, we are putting the custom parameters into the $custom variable for each article.