Keyword

Filter category items by tag.

  • V. Edmond
  • V. Edmond's Avatar Topic Author
  • Offline
  • New Member
More
8 years 11 months ago #143800 by V. Edmond
Filter category items by tag. was created by V. Edmond
Hi,

I would like to show in a category, posts that have the same city name tag. Like when I push the "Restaurants" menu button (linked to the restaurants category) I would like to display restaurants only from Tokio for example.

I was trying to achieve this using a query string in the URL: www.mysite.com/restaurants?Tokio, with somone's help I managed to modify the category.php template to show the results I need, but when I test it the itemListPrimary div is always empty and I can't figure out where is the problem.

I really don't now where to ask for help, hope someone could suggest a fix. Thanks!
<div id="itemListPrimary">

    <?php foreach ($this->primary as $key=>$item): ?>

        <?php
        # Get the value of the "tag" query string
        $jInput = JFactory::getApplication()->input;
        $myTag = $jInput->get('tag', null, 'STRING'); // Joomla 1.6+
        //$myTag = JRequest::getVar('tag'); // for Joomla 1.5

        # If the tag is empty, the query string is not specified and we'll go standard way without any tag filter
        if (empty($myTag)) {

            // Define a CSS class for the last container on each row
            if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
                $lastContainer= ' itemContainerLast';
            else
                $lastContainer='';
            ?>
            <div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
                <?php
                    // Load category_item.php by default
                    $this->item=$item;
                    echo $this->loadTemplate('item');
                ?>
            </div>
            <?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
            <div class="clr"></div>
            <?php endif;

        # Otherwise the tag is set so we'll filter the articles by the tag
        } else {

          # Get an array of all the tags that the current article in the loop has
          $articleTags = array();
          foreach ($item->tags as $tag) {
              $articleTags[] = $tag->name;
          }

          # Check if the article has the tag specified in the URL as a query string
          if (in_array($myTag, $articleTags)) {

              # Now the default content of the foreach loop comes as written in the default K2 category.php template

              // Define a CSS class for the last container on each row
              if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
                  $lastContainer= ' itemContainerLast';
              else
                  $lastContainer='';
              ?>
              <div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
                  <?php
                      // Load category_item.php by default
                      $this->item=$item;
                      echo $this->loadTemplate('item');
                  ?>
              </div>
              <?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
              <div class="clr"></div>
              <?php endif;
          }
        } ?>

    <?php endforeach; ?>
</div>

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

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 11 months ago #143802 by Krikor Boghossian
Replied by Krikor Boghossian on topic Filter category items by tag.
The primary items are not included in this view.

You need to adjust the leading items' code to work instead of the primary items.
<?php if(isset($this->leading) && count($this->leading)): ?>
		<!-- Leading items -->
		<div id="itemListLeading">
			<?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='';
			?>
			
			<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->leading)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_leading_columns'), 1).'%;"'; ?>>
				<?php
					// Load category_item.php by default
					$this->item=$item;
					echo $this->loadTemplate('item');
				?>
			</div>
			<?php if(($key+1)%($this->params->get('num_leading_columns'))==0): ?>
			<div class="clr"></div>
			<?php endif; ?>
			<?php endforeach; ?>
			<div class="clr"></div>
		</div>
		<?php endif; ?>

Do not get confused about the class on the div. This code returns primary items.

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