0

In my main navigation menu I have category links that are sub menu items:

Example:

Main Navigation: Listings

Sub menu category items: Boating, Camping, Fishing

So basically if there is no listing in boating then the category link is not there

As it stands right now if a user clicks on the sub menu category items and there is no listing in there it gives them the sorry nothing found thing. Which is confusing

Instead I am trying maybe create a simple plugin that will only display the sub-menu category navigation links if there is listings in them. If there is not listings in them it will not display the sub menu navigation category link. Seems pretty simple and I have tried looking around the web and on Wordpress but I can not seem to find anything. I am not a programmer as I am sure you have figured out but I am sure that I can achieve this with a little help. Thanks

1 Answer 1

0

you should try that with a custom Nav Walker. Here is a code example how it should work.

I check if the menu item is a category link. If yes, there is another check -> category count.
If the category don't have any posts the link won't be passed to the html output.

class Menu_Category_Count extends Walker_Nav_Menu {

    var $number = 1;

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $value . $class_names .'>';

        $atts = array();
        $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
        $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
        $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
        $atts['href']   = ! empty( $item->url )        ? $item->url        : '';

        $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );

        $attributes = '';
        foreach ( $atts as $attr => $value ) {
            if ( ! empty( $value ) ) {
                $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
                $attributes .= ' ' . $attr . '="' . $value . '"';
            }
        }

        // Get Category + Category Count
        $have_posts = true;
        if ($item->object == "category") {
            $category = get_category($item->object_id);
            // Does the category have any posts?
            if ($category->category_count == 0) $have_posts = false;
        }

        if ($have_posts) {
            $item_output = $args->before;
            $item_output .= '<a'. $attributes .'>';
            $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
            $item_output .= '</a>';
            $item_output .= $args->after;
        }

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

If you never dealed with a custom nav walker take a look at this tutorial at tutsplus.

1
  • I just got back to my computer. I will try this above and make a plugin of this and see how it works out. I am very excited. Will post pack soon. Again thanks. Dec 22, 2014 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.