I am wondering is it possible to have different headers depending on the different articles? e.g article ID 1 shows header A and article ID 2 shows header B. Currently I have modified the template header php file in tpl/blocks/header.php, which the header is hardcoded with html. Is there a way to use php in order to achieve the custom header?

My current header.php:

<?php defined('_JEXEC') or die; <header class="header js-header">SOMETHING</header> <?php endif; ?>

up vote 1 down vote accepted

There are numerous ways to customise something depending on something, so I can only answer by suggesting some ways to do some things.

First of all, generally in Joomla one tries to do minimal coding in the template, and instead one uses component overrides and modules so that output can be managed in the admin interface. That being said, many templates are hardcoded, so sometimes it makes sense to just say 'I've tried to be a good person but look where it's got me, im just gunna stick this in the template like nobody cares.'

Use the Article Id in a condition in the template

<?php
defined('_JEXEC') or die;

$jinput = JFactory::getApplication()->input;
$component = $jinput->get('option','','cmd');
$id = $jinput->get('id',0,'int');
?>
<header class="header js-header">
<?php
if($component == 'com_content'){

    switch ($id) {
        case '34':
            echo '<p>Some Content For Article Id 34</p>';
            break;
        case '48':
            echo '<p>Some Content For Article Id 48</p>';
            break;
        default:
            echo '<p>Some Default Content For other articles and category lists etc</p>';
            break;
    }

} else {
    echo '<p>Some Default Header Content For Pages that are not views of com_content</p>';

}
?>
</header>

Use a module position

You could instead use a module position and then insert your own custom modules in the position and assign them to menu items as appropriate.

<?php
defined('_JEXEC') or die; ?>

<jdoc:include type="modules" name="customheader" />

Use a custom field in the article and pull in the content

Perhaps more ergonomic but more involved to setup would be to add a custom field to the articles and then pull that content in, something like the following.

<?php
defined('_JEXEC') or die; 

$jinput = JFactory::getApplication()->input;
$component = $jinput->get('option','','cmd');
$view = $jinput->get('view','','cmd');

$header = 'some default content';

if($component == 'com_content' && $view == 'article'){

    JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
    JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');

    $id = $jinput->get('id');

    $model =& JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request'=>true));
    $appParams = JFactory::getApplication()->getParams();
    $model->setState('params', $appParams);
    $item =& $model->getItem($id);
    $jcFields = FieldsHelper::getFields('com_content.article',  $item, True);

    $name_of_your_custom_field = 'mycustomheader';

    foreach($jcFields as $jcField){
        if($jcField->name == $name_of_your_custom_field){
            $header = $jcField->value;
            break;
        }
    }

}

?>
<header><?php echo $header; ?></header>
  • Thank you, you're a legend! – Z Pat Nov 3 at 1:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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