4

Is it possible to change the template of a single article based on the category. The reason I ask is because I want to have a single article html override using fields and I don't want the client to have to remember to change the template.

Is it possible to hook into the template for an article in that way? And is it possible to listen to the category change in that way? Fields updates based on the category selected so I assume I can hook into the category using JS. Is it possible to change the template option?

2 Answers 2

2

Yes, it's possible. You can do it with a system plugin. However there is already a such solution available by Regular Labs. It's called Advanced Template Manager, and it works similarly to the Advanced Module Manager (if you have ever used it).

Advanced Template Manager is an extension that changes the way your Template manager works.

With Joomla you can normally only assign template styles to certain menu items.

With Advanced Template Manager you have the possibility to assign template styles to just about anything you want, giving you full control over your Joomla template styles.

Not compatible with...

Advanced Template Manager is NOT compatible with these (and maybe other) templates and template frameworks:

Gantry (RocketTheme)
JoomlaXTC
T3 (JoomlArt)
Warp (YooTheme)


If it happens to use one of those non-compatible templates/frameworks, it could still be possible to develop your plugin and handle this, but it will take some research to find out how these frameworks deal with their template styles.

This is a basic frame of what you should do in your system plugin:

public function onAfterRoute()
{
    $mobile_template_name = 'your-other-template';
    $app = JFactory::getApplication();

    // only run in front-end
    if (!$app->isAdmin())
    {
        $selectedCategories = $this->params->get('categories');
        $input = $app->input;

        // run for articles
        if ($input->get('option') === 'com_content' && $input->get('view') === 'article')
        {
            // get article model
            JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');
            $model   = JModelLegacy::getInstance('Article', 'ContentModel');
            $article = $model->getItem($input->get('id'));

            // find current article catid
            $catid = $article->catid;

            if (in_array($catid, $selectedCategories))
            {
                // article is in the selected Categories, so Set your other template
                $app->setTemplate($mobile_template_name);                       
            }
        }
    }
}
3
  • I think we may be on different pages here. I am talking about a single article template. A html override for com_content single article. So that wouldn't matter about my template right? Sorry if I was unclear.
    – Eoin
    Nov 30, 2017 at 23:36
  • 1
    @Eoin - sorry, I misunderstood your question.
    – FFrewin
    Dec 1, 2017 at 8:11
  • Thought so, no problems. I have reworded it hopefully that makes it clearer. Good answer all the same and as ever thank you for being so helpful and taking the time to answer.
    – Eoin
    Dec 1, 2017 at 13:13
0

One way I found to do this is to use an if statement in the single article html override. I'd prefer it in the file above the HTML override but I can't work out how to do that anymore.

if ($this->item->catid == 14):

// your stuff here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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