Keyword

Display on k2-items that are at least one year old?

  • Odin Mayland
  • Odin Mayland's Avatar Topic Author
  • Offline
  • Platinum Member
More
6 years 2 months ago - 6 years 2 months ago #169102 by Odin Mayland
Advanced Module Manager allows for a module assignment using custom php. The docs state:

Enter a piece of PHP code to evaluate. The code must return the value true or false.


I was planning to display a Custom-HTML module in the Below-Content position to display some tex on all k2-items in a specified category that are at least one year old?

It appears that this is the code that displays the date in a k2-item:

<?php if($this->item->params->get('itemDateCreated')): ?>
<!-- Date created -->
<span class="itemDateCreated">
<?php echo JHTML::_('date', $this->item->created , JText::_('K2_DATE_FORMAT_LC2')); ?>
</span>
<?php endif; ?>

I assume the custom PHP is going to be like this example:
return ( $user->name == 'Peter van Westen' );

Can you help me write the custom PHP to return a true for a k2-item that is at least one year old?
Last edit: 6 years 2 months ago by Odin Mayland.

Please Log in or Create an account to join the conversation.

More
6 years 2 months ago #169108 by JoomlaWorks
Replied by JoomlaWorks on topic Display on k2-items that are at least one year old?
It's better to do a K2 item override for this as no module can have access to the K2 item data object unless it retrieves it with an SQL query or uses the K2 item model.

If you do an override, you can have what you want like this:
<?php
if ($this->item->category->id = CATEGORY_ID_HERE) {
    $createdDate = strtotime($this->item->created);
    $oneYearBack = strtotime('now -1 year');
    if ($createdDate > $oneYearBack) { // Item is at least a year old
        // do something
    }
}

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • Odin Mayland
  • Odin Mayland's Avatar Topic Author
  • Offline
  • Platinum Member
More
6 years 2 months ago - 6 years 2 months ago #169120 by Odin Mayland
Replied by Odin Mayland on topic Display on k2-items that are at least one year old?
Thank you! I got it to work be switching the > to <
<?php
if ($this->item->category->id = 48) {
    $createdDate = strtotime($this->item->created);
    $oneYearBack = strtotime('now -1 year');
    if ($createdDate < $oneYearBack) { echo JHtml::_('content.prepare', '{loadposition disclaimer}'); 

    }
}
?>
Last edit: 6 years 2 months ago by Odin Mayland.

Please Log in or Create an account to join the conversation.

More
6 years 2 months ago #169123 by JoomlaWorks
Replied by JoomlaWorks on topic Display on k2-items that are at least one year old?
Ah, sorry, kinda read it like "less than a year old" (stupid me).

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum