2

I am using Jommla's default Protostar template. I have custom fields for articles and I want to show this custom field in the header of the page, not inside the article. How can I achieve that?

2

1. Turn off automatic display

You will want to set the Automatic Display in the field's Options tab to "Do not automatically display". This will make it not show in the HTML body as part of the article.

2. Create article override.

Next, you will need to create an override of the article view, if it does not already exist. Copy default.php and default_links.php from components/com_content/views/article/tmpl/ and past them into templates/YOUR_TEMPLATE/html/com_content/article/.

These files will override the default component article view files.

3. Add Custom Field to HTML head

To add the contents of the custom field to the <head> of the page you will need to edit the default.php file you just created.

If you were adding a canonical tag it would look a little like this.

//Insert custom field into HTML head if not empty
foreach ($this->item->jcfields as $field) {
    //Replace X with the field ID number
    if ($field->id === 'X') {
        //Makes sure the field is not empty
        if (!empty($field->rawvalue)) {
            //Inserts the field content with markup into the HTML head
            $customField = '<link href="' . $field->rawvalue . '" rel="canonical" />';
            $document = JFactory::getDocument();
            $document->addCustomTag($customField);
        }
        continue;
    }
};

You will need to provide the correct HTML markup in your code. You will need to replace $customField = '<link href="' . $field->rawvalue . '" rel="canonical" />'; with the correct markup.

For instance, if you wanted to include a Twitter Card author you would use something like this $customField = 'rawvalue . '"/>';`

Also note: you will need to replace the X with the ID of your custom field.

Lastly, I would rename $customField to something a little more relavent.

  • Many thanks for the detailed answer. I will try that and get back to you. – Rahman Sharif Apr 25 '18 at 21:33
  • You may find the Joomla documentation on Custom Field Overrides helpful. – D. Morell Apr 26 '18 at 16:15
  • I tested it, but I am getting the same error I got earlier. It is working perfectly inside the default.php file in protostar template, however, this file isn't responsible for the header. The header part is getting rendered through the index.php file, when I put this code, or the code provided by Joomla's documentation, I am getting the error: Undefined property: Joomla\CMS\Document\HtmlDocument::$item in C:\wamp\....\protostar\index.php – Rahman Sharif Apr 26 '18 at 18:50
2

You need to get the article using Article model. Then get its fields using FieldsHelper. One downside here is that article is not cached here so we get duplicate query.

// Check that we are on article view.
if ($option === 'com_content' && $view === 'article')
{
    $context = 'com_content.article';

    // Get the article.
    $article = JModelLegacy::getInstance('Article', 'ContentModel')->getItem();

    // Get article fields.
    JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
    $fields = FieldsHelper::getFields($context, $article);

    // We have the fields, do something, e.g.
    echo FieldsHelper::render($context, 'fields.render', array('context' => $context, 'item' => $article, 'fields' => $fields));
}

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.