Publish date in all articles but 2? Topic is solved

General questions relating to Joomla! 5.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
SocketPup
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 134
Joined: Thu Mar 14, 2024 7:00 am

Publish date in all articles but 2?

Post by SocketPup » Sun Apr 07, 2024 7:44 am

In my Joomla 5 project, I added a template override for articles.
It all seems to work well, and while I want the published-date to appear in most articles, I also want to omit them in articles that are on my Home section, and in the section with the alias "om-oss."

What I did so far is I replaced the default code for displaying the published-date with this code:

Code: Select all

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();

// Check if the active menu item is the default home page
$isDefaultHomePage = $active && $active->home == 1;

if (!$isDefaultHomePage) {
    if ($info == 1 || $info == 2) :
        if ($useDefList) :
            echo LayoutHelper::render('joomla.content.info_block', ['item' => $this->item, 'params' => $params, 'position' => 'below']);
        endif;
        if ($params->get('show_tags', 1) && !empty($this->item->tags->itemTags)) :
            $this->item->tagLayout = new FileLayout('joomla.content.tags');
            echo $this->item->tagLayout->render($this->item->tags->itemTags);
        endif;
    endif;
}
?>
It works well for the Home section. But how do I also make it not show up in the article with the "om-oss" alias?

I tried this solution, but nothing changed:

Code: Select all

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();

// Check if the active menu item is the default home page
$isDefaultHomePage = $active && $active->home == 1;

// Check if the article alias is "om-oss"
$isOmOssAlias = $app->input->get('alias') === 'om-oss';

if (!$isDefaultHomePage && !$isOmOssAlias) {
    if ($info == 1 || $info == 2) :
        if ($useDefList) :
            echo LayoutHelper::render('joomla.content.info_block', ['item' => $this->item, 'params' => $params, 'position' => 'below']);
        endif;
        if ($params->get('show_tags', 1) && !empty($this->item->tags->itemTags)) :
            $this->item->tagLayout = new FileLayout('joomla.content.tags');
            echo $this->item->tagLayout->render($this->item->tags->itemTags);
        endif;
    endif;
}
?>

SocketPup
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 134
Joined: Thu Mar 14, 2024 7:00 am

Re: Publish date in all articles but 2?

Post by SocketPup » Sun Apr 07, 2024 11:48 am

Thanks for the improved solution, it was very neat and tidy, and seems to work well, except for one thing:

If no date is displayed, no other content after the code above is displayed either. There is a bunch of php code writing out other things after, and they are only being displayed in the articles where the dates are being displayed.

Also, I replaced catid with alias instead. Is that considered bad practice?
If I by accident delete the article(s) or categories with said id's, they will be designated new ones after I re-create them. This is not a huge issue, but if I instead could refer to their respective alias, I wouldn't need to alter the php code in such a case.

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 31084
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Publish date in all articles but 2?

Post by Per Yngve Berg » Sun Apr 07, 2024 1:55 pm

I don't know why you are making overrides. Just turn off display of the attribute in the Menu item.

SocketPup
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 134
Joined: Thu Mar 14, 2024 7:00 am

Re: Publish date in all articles but 2?

Post by SocketPup » Sun Apr 07, 2024 5:04 pm

Per Yngve Berg wrote: Sun Apr 07, 2024 1:55 pm I don't know why you are making overrides. Just turn off display of the attribute in the Menu item.
Thanks, that was an even better solution.


Post Reply

Return to “General Questions/New to Joomla! 5.x”