Forum

Thread tagged as: Question

Filter categories with no entries

Hi

Would like to know if its is possible to filter out blog categories that have no entries associated to them?

Currently my php just outputs all categories within the Blog set.

php:

<?php 
            PerchSystem::set_var('current_category', perch_get('cat'));
            perch_categories(array( 
                'set'=>'blog',
                'template' => 'category_menu.html',
                 ));
        ?>

Template:

<perch:before>
            <nav class="navigation clear" role="navigation">
                <div class="inner shrink-menu">
                    <ul class="visible-links">
                        <li><i class="mdi mdi-filter"></i><span class="label">Product:</span></li>
                        <li id="all"><a href="/"><span>All</span></a></li></perch:before>
                        <li><a href="/blog/category/<perch:category id="catSlug" />"<perch:if id="catSlug" match="eq" value="{current_category}"> class="current"</perch:if>><span><perch:category id="catTitle" /></span></a></li><perch:after>
                    </ul>
                </div>
            </nav>
            </perch:after>

Originally I was just using perch_blog_categories(); which did the trick, but I wanted to add a class of "current" to a selected category and couldn't get this to work without changing to perch_categories();

Many thanks, Grant

Grant Purtle

Grant Purtle 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you should be able to get your category selection to work - you just need to be mindful of the difference between a category slug and a category path.

Thanks, this post helped with the 'current' class - although I too am wondering how to hide any categories that will have 0 results?

It's been a couple months and I'm back revisiting this project.

I have the current category selection working as before, however I am still wondering how to either:

  1. Only output the categories for the blog set which have posts associated to them, or
  2. Output all categories, but when you view the archive of a category it says something like "No posts to display" - current this is just blank.

The first option sounds better to me?

Code is still the same as above.

Grant

Drew McLellan

Drew McLellan 2638 points
Perch Support

If you use perch_blog_categories() you can use the include-empty option.