• Clumsy title, but here is what I mean. And I really hope someone will read all this 😉

    I have three top level categories, ie Subject, Countries, Regions. Each post is asigned to three categories, each being one of the subcategories of those top level categories, ie Animals, France, Region 12. But now I have to populate some lists so I can create maps with CSS for my navigation. In the end people will see a page with animals inside France inside region 12.

    The first level is easy and on index.php:

    $subjects = get_categories('hide_empty=0&child_of=2');
    foreach($subjects as $subject) {
    	$test = get_posts('category='.$subject->cat_ID);
    	if($test) {
    	echo "<li><a href=\"" . get_category_link($subject->cat_ID) . "\"><span>". get_cat_name($subject->cat_ID) ."</span></a></li>";
    	} else {
    	echo "<li class=\"maincatinactive\"><span>". get_cat_name($subject->cat_ID) ."</span></li>";
    	}
    }

    But I’m really stuck on the second and third part of my navigation. When I’m on the category.php page I first want to show the countries that have posts AND are inside the previously chosen subcategory Animals. And after clicking on a country I want to create a map of the chosen country with regions and show only the regions in color that have posts AND are inside category France AND inside category Animals. And I have no idea how to accomplish that. I need some way to check at each reloading of the category.php page if that subcat has posts AND belongs to one of the other categories.

    Any ideas on how to do this?

Viewing 15 replies - 1 through 15 (of 42 total)
  • Thread Starter Jack

    (@moxie)

    I think I want to rephrase my question to explain what is really needed in my case:

    1. Category structure: 3 main categories.
    2. Each main category has several sub categories
    3. Each post belongs to a unique set of 3 sub categories, one from each main category

    Navigation will go through each main category seperately. After choosing a subcategory of the first main category, a choice can be made for a subcategory of the second main category and after that from within the third main category.

    The problem is that each subsequent choice has to ‘remember’ the previous choice. How do I do that? In the end I need a page with the only 5 posts that belong to those 3 choices.

    Moderator keesiemeijer

    (@keesiemeijer)

    You can check the parent category as the “the previous choice”.
    somthing like this: example

    Thread Starter Jack

    (@moxie)

    Hello Keesie. and thanks for your reply, but I’m a little confused about the code. After choosing one of the subcats of the first top level cat, as shown at the top of this page, I need to make of choice for one of the subcategories of the second top level cat. So, then $subject= get_categories('child_of='.$current_cat); would return the subcats of the first choice, which doesn’t have subcats because it is a subcat.

    So:

    Animals
    – Pigs
    – Cats
    Countries
    – France
    – England
    Regions
    – Region 1
    – Region 2

    A post belongs to a combination of three subcats:
    Pigs/England/Region 1
    Pigs/France/Region 1
    Cats/England/Region 1
    Cats/England/Region 2 etc.

    If there are posts that belong to subcat Pigs, first I want to present a map of countries (all the subcats of Countries), and show which one has regions that has pigs or not (countries that do are colored, the ones that don’t are grey). Still I don’t want to show posts. After clicking a country I present a map of that country divided by regions, and again regions that do are colored, the ones that don’t are grey. After clicking the region (that has posts that belong to the specific animal and the specific country and specific region) I want to show the results.

    Is this possible? Or sorry if I didn’t understand your code 😉

    Moderator keesiemeijer

    (@keesiemeijer)

    I would try with a structure like this. Then you can test for the previous choice. And this way a region for England cannot be a region for France.

    Animals (top level cat)
    – Pigs
    – Cats
    Countries (top level cat)
    – Europe
    — France
    — Region 1
    — England
    — Region 1
    — Region 2

    Thread Starter Jack

    (@moxie)

    I followed your suggestion and changed the whole category structure and I can see the benefits of this. Now each post is assigned to only two categories: a subcat of Animals and one of the regions. The only thing I still don’t understand is how I can check on the category.php page if a country/region has posts that are also inside the one specific animals cat.

    With the code I have now, on the page that would show the map of the country, it will show all countries that have posts and not only to the first chosen category. It will show me all countries that has animals, not just one of them.

    $cat_id = get_query_var('cat');
    $animal = get_category( $cat_id ); // the previously chosen animal
    $countries = get_categories('hide_empty=0&exclude=1&orderby=slug&order=ASC&parent=6'); // which is the category id of countries
    echo "<ul>";
    foreach($countries as $country) {
    	$test=get_posts('category='.$country->cat_ID);
    	if ($test) {
    	echo "<li class=\"".$country->cat_name."\"><a href=\"" . get_category_link($country->cat_ID) . "\"><span>". get_cat_name($country->cat_ID) ."</span></a></li>";
    	} else {
    	echo "<li class=\"".$country->cat_name."\"></li>";
    	}
    }
    echo "</ul>";

    After that, I’m facing the same problem with showing the regions that have posts that are also inside that particular category.

    I hope you can give me some tips on how to do this. I’m struggling with this for weeks now 😉

    Moderator keesiemeijer

    (@keesiemeijer)

    Try this next example: example

    change this id $country_cat_id = 51; to the category id of your top level category “Countries”.

    Thread Starter Jack

    (@moxie)

    Thanks for all that code 🙂
    Before testing it, I see that you embedded part of the code for continents, but I don’t have continents, just countries and regions. Could you tell me which part of the code I should remove then? And just to be sure, am I correct that all this should go inside category.php?

    Also, just noticed that for each list there is no list for if there are no posts. For those I use just empty list items so I can style them with css and they will be grey and not clickable.

    Moderator keesiemeijer

    (@keesiemeijer)

    but I don’t have continents, just countries and regions.

    So, the first map after clicking a sub level category (pigs) is a map of Europe then? Or do you want to have links to continents?

    And just to be sure, am I correct that all this should go inside category.php?

    yes

    This example just shows the navigation for what you want. For empty list items you can do something like this:

    $countries  = get_categories('hide_empty=0&parent='.$_GET['place_id']);
     foreach($countries as $country) {
     if($country->count == 0) {
     // no posts
     } else {
     // has posts
     }
     }

    But first lets get the navigation right before checking if a category has no posts.

    Thread Starter Jack

    (@moxie)

    Well, to be precise, those categories were just an example to explain on this forum how the website is supposed to work. But because you are helping me so much I think I will explain the actual structure 🙂

    Animals
    – Pigs
    – Cats
    Provinces
    – First province name
    — Region 1
    — Region 2
    – Second Province name
    — Region 1
    — Region 2

    So the first map after clicking ‘Pigs’ (which is a sub level category of top level category ‘Animals’) is actually a map of one single country and the only country. This country is divided into provinces (the second top level category) which are divided into regions. So first one clicks on the link ‘Pigs’ (on index.php). Secondly there is the map of the country with clickable and unclickable provinces, depending on if a region inside that province has posts that are also part of the category ‘Pigs’. After that a map of the province is shown, divided into clickable and unclickable regions, depending on (again) if a region inside that province has posts that are also part of the category ‘Pigs’.

    I hope this makes things more clear for you.

    Moderator keesiemeijer

    (@keesiemeijer)

    try this example: example
    change this id $profinces_cat_id = 51; to the category id of your top level category “Provinces”. And use the category structure you have in your last post.

    Thread Starter Jack

    (@moxie)

    It doesn’t work exactly right yet. Whenever I’m on a page with the country map all the provinces are colored that have posts, even the ones that don’t belong to the chosen category, ie cats or pigs.

    I removed the first two ‘issets’ because I think I don’t need them (correct me if I’m wrong), because on the index.php page I already made a choice like this:

    <li><a href=\"" . get_category_link($subject->cat_ID) . "?show=map&type=provinces\">

    Also I replaced the -> == 0 by

    $postsinregions = get_posts('hide_empty=0&category='.$province->cat_ID);
    if(!$postsinregions) {

    because somehow it didn’t work properly.

    For now I have this code, including some css I need to create the maps: code. I think it will work, except for the check if the posts belong to the first category. I think you are trying to do that with

    get_posts(array( 'category__and' => array( $regio->cat_ID, $current_cat ) ) );
    Am I correct?

    Thread Starter Jack

    (@moxie)

    I’ve been playing with your code, and as far as I can see, the only thing that isn’t working yet is that check: do the posts that are inside the specific province or region ALSO belong to the first cat.

    I placed your code on several places, but until now with no effect.

    $sublevel_cat = get_posts(array( 'category__and' => array( $province->cat_ID, $current_cat ) ) );

    Moderator keesiemeijer

    (@keesiemeijer)

    Did you give the posts the category (example): pigs, province, region? You don’t have to give it the top level “provinces” category.

    Thread Starter Jack

    (@moxie)

    No, just pigs and region, no province because the region is already part of that province.

    Thread Starter Jack

    (@moxie)

    Somehow there is a part missing.
    After or just before the foreach() there should be a check like the category__and, because with every page there still is a cat between the pigs 😉

Viewing 15 replies - 1 through 15 (of 42 total)
  • The topic ‘How to check if three different categories have posts and act upon each of them’ is closed to new replies.