• Resolved pedroliveira

    (@pedroliveira)


    I have a question that I’m aware it’s rather complex, that’s why I haven’t found any similar questions, so… here we go.

    Fisrt let me explain the structure I’m dealing with:

    All my Content is divided into two Parent Categories: “Art” and “Levels”.

    “Art” has child categories, which are names for Works. Like “Duchamp’s Ready-Made” and so on.

    Each Work has a bunch of regular posts, organized by its “Levels” which are descriptions of its contents. For instance, “Music” or “History” and so on.

    So, if I search for a Work, it will return me all the Posts in this Work category, sorted and organized by its Levels.

    Ok, I know, it’s complex. And here’s the deal:

    I want that, when viewing a Work Page (category-id.php), the menu shows me ONLY the Child categories of “Levels” that are associated with this particular Work. So, if I have the Level “The Problem” and “Duchamp” has no posts with this Level, I don’t want the menu to show it.

    But, what makes it difficult is that each Level is an anchor, so, here’s my code:

    function showNiveis() {
    	$listaNiveis = get_categories('child_of=4&orderby=ID&hide_empty=true');
    	foreach($listaNiveis as $niveis) {
    			echo '<li class="nivelMenu"><a href="#' . $niveis->category_nicename . '" class="execScroll">' . $niveis->cat_name . '</a></li>';
    	}
    }

    where “childof=4” are the children for the “Levels” category.

    Sorry, I know it’s difficult, that’s why I’m banging my head against the wall to solve it.

    Thanks in advance!

Viewing 1 replies (of 1 total)
  • Thread Starter pedroliveira

    (@pedroliveira)

    Ok, I already figured it out by myself, just thinking it out ??

    If anyone is interested, I solved it by doing a “query_posts(array(category__and” using the category for the Work and $niveis->cat_ID to filter the query, only showing one post each, right before the “echo” line.

    Then you have to reset the query by calling wp_reset_query();

    here’s the code:

    function showNiveis() {
    	$catMenu01 = get_query_var('cat');
    	$catMenu02 = get_category($catMenu01);
    	$catMenuFinal = $catMenu02->cat_ID; // grabbing the requested category ID
    
    	$listaNiveis = get_categories('child_of=4&orderby=ID&hide_empty=true');
    	foreach($listaNiveis as $niveis) {
    		query_posts(array('category__and'=>array($catMenuFinal,$niveis->cat_ID),'showposts'=>1));
    		if(have_posts()) : while(have_posts()) : the_post();
    			echo '<li class="nivelMenu"><a href="#' . $niveis->category_nicename . '" class="execScroll">' . $niveis->cat_name . '</a></li>';
    	    endwhile;
    		endif;
    		wp_reset_query();
    	}
    }

    and voilá!

Viewing 1 replies (of 1 total)
  • The topic ‘Complex Menu with Child Categories Filter’ is closed to new replies.