Keyword

[SOLVED] K2 Category - How to detect if item is featured

More
11 years 3 months ago #115095 by Vseva
HI;

Here's what I'm trying to do:

I want to add some custom code so that when the featured items (which I have set to display first) have a title ex: 'Featured items'

AND

When they aren't featured I want the other (non-featured) items to have a different title ex: 'Non featured items'

So basically I'm trying achieve this by adding an if statement just before the itemListhead.
The problem is I'm not sure what the correct php functions to call ?
Here's what I've tried, but that do not work:

if ($item->featured) ...
if ($this->leading->item->featured) ...
if($params->get('FeaturedItems'))
if($this->leading->params->get->featured)

So, does anybody know how I can find out if an item is featured from the category page?

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
11 years 3 months ago #115096 by Krikor Boghossian
Replied by Krikor Boghossian on topic Re: K2 Category - How to detect if item is featured
Hello Vseva

You need to edit the category_item.php
					
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

More
11 years 3 months ago - 11 years 3 months ago #115097 by Vseva
HI Krikor;

Thanks for the prompt reply.

Sorry about the confusion there, I realized I had created the <div class='itemListHead'> and I placed it on the category.php view page.

Nevertheless, I can't place on the category_list.php view as on the category.php there is a foreach loop that would repeat the title over and over for each item.

What I trying to do is place it before the foreach loop and detect wether the leading items are featured or not, and then display the correct title with a conditional statement.
<?php if($params->get('FeaturedItems')): ?>
                <div class="itemListHeadFeatured">
                    <h2><?php echo JText::_('K2_ITEM_LIST_HEAD'); ?></h2>
                </div>
                        <?php foreach($this->leading as $key=>$item): ?>

                        <?php
                        // Define a CSS class for the last container on each row
                        if( (($key+1)%($this->params->get('num_leading_columns'))==0) || count($this->leading)<$this->params->get('num_leading_columns'))
                                $lastContainer= ' itemContainerLast';
                        else
                                $lastContainer='';
                        ?>
            <?php else: ?>
                <div class="itemListHead">
                    <h2><?php echo JText::_('K2_ITEM_LIST_HEAD'); ?></h2>
                </div>
                        <?php foreach($this->leading as $key=>$item): ?>

                        <?php
                        // Define a CSS class for the last container on each row
                        if( (($key+1)%($this->params->get('num_leading_columns'))==0) || count($this->leading)<$this->params->get('num_leading_columns'))
                                $lastContainer= ' itemContainerLast';
                        else
                                $lastContainer='';
                        ?>
            <?php endif; ?>

of course this code will not work , as is, as the it's not calling the right function that would validate that the items are featured.

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
11 years 3 months ago #115098 by Krikor Boghossian
Replied by Krikor Boghossian on topic Re: K2 Category - How to detect if item is featured
Which version of K2 are you using? By default K2 has category.php and category_item.php, I assume category_list.php is a custom template part.

You cannot use this snippet outside the foreach loop. What you can do is use the $key variable to show it just for the first item.
<?php if($key==0) :?>

This however will only output the title if the first item is featured.
You might need to use a different approach (like a counter) if you wish to calculate the number of featured items.

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

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

More
11 years 3 months ago #115099 by Vseva
Hey Krikor;

Thanks for the hint as it helped me find the solution.
I was toying with a counter, and eventually got it to work !!!

So I thought I'd might as well share it with my follow K2 -ist !!
(place this code in the itemListLeading div foreach loop in the category.php
around line 152.)
And of course , you will have to make 2 language overrides called:
K2_ITEM_LIST_HEAD_FEATURED
K2_ITEM_LIST_HEAD

Replace this :
<?php
	// Load category_item.php by default
	$this->item=$item;
	echo $this->loadTemplate('item');
?>

With this:
    <?php 
                 // Load category_item.php by default
                 $this->item=$item; 
    ?>
     <?php  if($item->featured == 1): ?>
               <?php  $x++; ?>
                <?php  if($key == 0): ?>
                          <div class="itemListHead">
                              <h2><?php echo JText::_('K2_ITEM_LIST_HEAD_FEATURED'); ?></h2>
                          </div>
                 <?php endif; ?>
      <?php elseif($item->featured == 0): ?>
               <?php  if($key == $x): ?>
                           <div class="itemListHead">
                              <h2><?php echo JText::_('K2_ITEM_LIST_HEAD'); ?></h2>
                           </div>
                <?php endif; ?>
        <?php endif; ?>
        <?php echo $this->loadTemplate('item'); ?>

It even works with pagination too !
Hope this is useful for someone.

Thanks for the help Krikor.

Peace.

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

More
11 years 5 days ago #115100 by Askmail 36
How can i display, when i make a research, a simple "featured item" instead of the read more near the title of the articles??

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


Powered by Kunena Forum