• ‘m working on a wordpress site for a restaurant that has multiple locations so my categories are set up like this:

    Locations (parent category)
    -Location 1
    -Location 2 etc. (child categories)

    Menu Items (parent category)
    -Type 1
    -Type 2 (child catgegories)

    All menu posts (custom Post type) are assigned at least one Location category and at least one Menu Item Type (pizza, burgers, beers)

    SO I need to hide a Menu Item Type if it does not have any posts for that particular location. So location 1 could have pizza, burgers and beers and location two may only have pizza and beers.

    <?php $thePage = $post->post_title; echo $thePage; ?>

    <?php

    $theLocations = array(
    ‘posts_per_page’ => 100,
    ‘post_type’ => ‘food-items’,
    ‘category_name’ => $thePage //Location name (Location 2, etc.)
    );

    query_posts($theLocations);

    // get all the categories from the database
    $cats = get_categories();
    // loop through the categries
    foreach ($cats as $cat) {

    $cat_id= $cat->term_id;

    if(cat_is_ancestor_of(’19’, $cat)){

    echo “<h1>”.$cat->name.”</h1>”;
    //How do I hide this if this category (burgers)
    //is empty for this location (location 2)?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    First of all, don’t use query_posts(). It basically doubles the server load. It may not be a big deal on lightly trafficked sites, but anything to reduce server response times is important from a usability standpoint. Also, there is no need, anything you can do with query_posts() can be done through the "pre_get_posts" action hook.

    To get only category terms that have posts use get_terms(). The “hide_empty” argument defaults to TRUE so the only argument you may need is for the taxonomy “category”.

    Thread Starter miker42

    (@miker42)

    @bcworkz Thanks for the reply.

    I think get_terms() is close but my situation is this: I may have a location that has three items and a location that has two items like so…

    I need to hide a Menu Item Type if it does not have any posts for that particular location. So location 1 could have pizza, burgers and beers and location 2 may only have pizza and beers.

    I need to hide the burgers title when I’m listing items for location 2.

    Moderator bcworkz

    (@bcworkz)

    Ah, right. I was working off your code that gets categories and lost sight of the big picture.

    You can still leverage the ability of get_terms() to screen out categories with no posts. Hook the ‘wp_nav_menu_objects’ filter, which passes all of the nav menu item objects before any nav menu is output. In your callback, first call get_terms() using ‘hide_empty’ as I suggested. Also use the ‘fields’ argument to just get the term IDs in an array. (Verify it’s not an array of arrays, if so, process the IDs into a single array)

    Loop through all of the passed menu objects. If $menu_object->object_id is set and is NOT in the array of term IDs, then remove the current object from the array that’s returned from your callback.

    Thread Starter miker42

    (@miker42)

    So i did finally solve it. With a counter inside the loop. Here’s part of the code:
    $theCount = 0; //this gets reset with each new category
    if (have_posts()) : ?>
    <?php $theCount = 0; ?>
    <?php while (have_posts()) : the_post(); ?>

    <?php if ( in_category($cat->name) ) { ?>
    <?php if ($theCount == 0) echo “<h1>”.$cat->name.”</h1>”; ?>
    <?php $theCount++; ?>

    Worked like a charm.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How do I hide an empty category outside the loop?’ is closed to new replies.