title: Add Meta Keyword condition to IF in Magento - Edit01
body: <p>The intention of this code (1) is to display the subcategories in a Magento store in grid form. The size of the category images is defined in the code itself while the separation and width is in styles.css.</p>
  
  <p>If I use the code normally I get all the sub-categories that are enabled (IsActive is true). See this "work in progress" link: <a href="http://www.aldetal.biz/index.php/vabaro/fitness/nutricion.html" rel="nofollow">http://www.aldetal.biz/index.php/vabaro/fitness/nutricion.html</a></p>
  
  <p>I'd prefer to display the sub-categories that are enabled AND are flagged elsewhere as "display worthy". That way I separate how many subcategories appear in grid form in the landing page from how many appear in the menu. To avoid touching the database I'll be using the Meta Keywords field adding the string showgrid as a "keyword" flag. </p>
  
  <h2>Code</h2>
  
  <pre><code>&lt;?php
  /** 
  * Original code by Jake Rutter
  * @website: http://www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.html#idc-ctools
  * Fixed by func0der
  *
  */
  ?&gt;
  
  &lt;div id="categories"&gt;
      &lt;div class="col_full"&gt;
          &lt;div class="listing" &gt;
          &lt;?php
              $_maincategorylisting = $this-&gt;getCurrentCategory();
              $_categories = $this-&gt;getCurrentChildCategories();
  
              if($_categories-&gt;count()):
                  foreach ($_categories as $_category):
  
      /** Choose between the two possible IFs. The first one works (as before, incompletely. The second Should work completely but instead breaks the code */
      /**                 if($_category-&gt;getIsActive()): */ 
                          if($_category-&gt;getIsActive() &amp;&amp; preg_match('/^showgrid/ism', $_category-&gt;getData('meta_keywords'))): 
  
                          $cur_category=Mage::getModel('catalog/category')-&gt;load($_category-&gt;getId());
                          $layer = Mage::getSingleton('catalog/layer');
                          $layer-&gt;setCurrentCategory($cur_category);
  
      /** This is just for debugging. The first shows the sub-category name under the image (as it should in final version). The second one shows the text in Meta_Keywords. */
      /** 
      /**                 $catName = $this-&gt;getCurrentCategory()-&gt;getName(); */
                          $catName = $this-&gt;getCurrentCategory()-&gt;getData('meta_keywords'); 
  
                          if($_imageUrl = $this-&gt;getCurrentCategory()-&gt;getImageUrl()):
              ?&gt;
  
              &lt;div class="category-box"&gt;
                  &lt;div class="category-image-box"&gt;
                      &lt;a href="&lt;?php echo $this-&gt;getCategoryUrl($_category)?&gt;"&gt;&lt;img src="&lt;?php echo $_imageUrl?&gt;" height="150"&gt;&lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="category-name"&gt;
  
                      &lt;?php
                      /**  Changed to check writing the category vs variable */
                      /** &lt;a href="&lt;?php echo $this-&gt;getCategoryUrl($_category)?&gt;"&gt; &lt;?php echo $_category-&gt;getName()?&gt;&lt;/a&gt; */
                      ?&gt;
  
                      &lt;p&gt;
                          &lt;a href="&lt;?php echo $this-&gt;getCategoryUrl($_category)?&gt;"&gt; &lt;?php echo $catName ?&gt;&lt;/a&gt;
                      &lt;/p&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;?php 
                          else:
              ?&gt;
              &lt;div class="category-box"&gt;
                  &lt;div class="category-image-box"&gt;
                      &lt;a href="&lt;?php echo $this-&gt;getCategoryUrl($_category)?&gt;"&gt;&lt;img src="/skin/frontend/default/default/images/media/default01.jpg" height="150"); ?&gt;&lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="category-name"&gt;
                      &lt;p&gt;
                          &lt;a href="&lt;?php echo $this-&gt;getCategoryUrl($_category)?&gt;"&gt;&lt;?php echo $catName ?&gt;&lt;/a&gt;
                      &lt;/p&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
  
              &lt;?php 
                          endif; /* END: if($_imageUrl=!$this-&gt;getCurrentCategory()-&gt;getImageUrl()) */
                      endif; /* END: $_category-&gt;getIsActive()) */
                  endforeach; /* END: ($_categories as $_category) */
  
                  /* This resets the category back to the original pages category
                  *    If this is not done, subsequent calls on the same page will use the last category in the foreach loop.
                  * 
                  *    The next line is the one showing the problem. If I use the expanded IF I get the error here...
                  */
  
                  $layer-&gt;setCurrentCategory($_maincategorylisting);
                  endif; /* END: if($_categories-&gt;count()) */
              ?&gt;
          &lt;/div&gt;
          &lt;br clear=all&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <hr>
  
  <p>For some reason the excellent IF suggested by Tim </p>
  
  <p>(1) Originally written by Jake Rutter here: <a href="http://www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.html" rel="nofollow">http://www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.html</a> and tweaked by me</p>
  
  <hr>
  
  <p>I have code with the following construct to display sub-categories in Magento front end. The sub-categories display when they are enabled ("IsActive" is true). </p>
  
  <pre><code>  if($_categories-&gt;count()):
   foreach ($_categories as $_category):
          if($_category-&gt;getIsActive()):
                 a whole bunch of stuff if true
              else
                 other stuff if not true                 
  </code></pre>
  
  <p>I want to add another condition without messing with the database so I'm using the Meta Keywords field (because it is officially ignored by Google and it doesn't do any harm to have a quirky keyword anyhow). </p>
  
  <p>If the category is enabled (IsActive) AND the first string in that field is "showgrid" the IF is true and the whole bunch of stuff happens. </p>
  
  <p><strong>BUT careful:</strong> the Meta Keywords field could be empty so I need to test it as well as determine its content. If the category is not enabled (IsActive is false) OR Meta Keyword is empty OR Meta Keyword does not begin with showgrid then the other stuff happens</p>