Forums

Echo some output foreach of categories children (11 posts)

  1. Jack Tummers
    Member
    Posted 1 year ago #

    I have an array called $regions.
    When I'm on a category page I want to output some custom links like this:

    if (is_category()) {
    	$cat_id = get_query_var('cat');
    	$catlist = get_categories('child_of=' . $cat_id);
    	if (get_category_children($catlist->cat_ID) != "") {
    		foreach(get_the_category($cat_id) as $catlist) {
    		$i=0;
    		echo $regions[$i] . " href=\"index.php?cat=" . $category->cat_ID . "\" alt=\"" . $category->cat_name . "\">";
    		$i++;
    		}
    	}
    }

    This doesn't work, but I'm not really a programmer so I don't see my mistake. Anyone?

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    can you describe with words what you are trying to do in the various steps in your code?

  3. Jack Tummers
    Member
    Posted 1 year ago #

    Of course :)

    I have categories which have categories which have categories = a hierarchy of three levels. I show these categories as lists on each subsequent page, except for the third page, which would be the list of the sub-sub-categories, I want to show a jpg instead of normal links. This jpg image then has several 'hotspots', like this:
    <area shape="poly" coords="94,134,158......> etc. I createad those hotspots with Gimp and copied the source code. Now I want to populate the image map with the correct hotspots, combined with the correct link to the sub-sub-category.

    In the example above I forgot to imclude the part of the map, so this was my attempt:

    <?php
    $regions = array(
    '<area shape="poly" coords="94,134,158,282,124,374,47,364,7,169,55,150"',
    '<area shape="poly" coords="128,99,231,100,283,276,161,283,97,128"',
    etc....
    ?>
    
    <?php
    echo "<map name=\"map\">";
    if (is_category()) {
    	$cat_id = get_query_var('cat');
    	$catlist = get_categories('child_of=' . $cat_id);
    	if (get_category_children($catlist->cat_ID) != "") {
    		foreach($cat_id as $catlist) {
    		$i=0;
    		echo $regions[$i] . " href=\"index.php?cat=" . $category->cat_ID . "\" alt=\"" . $category->cat_name . "\">";
    		$i++;
    		}
    	}
    }
    echo "</map>";
    ?>

    Please let me know if I have to clarify some more ;)

  4. Jack Tummers
    Member
    Posted 1 year ago #

    Could someone please have a look at this code? I want to combine the information in the first array with links to child cats in the second array. But the only thing I get is the first link....

    http://pastebin.com/1nFxfXvR

    Also.. the links have to be permalinks, and not the way I did it :(

  5. alchymyth
    The Sweeper
    Posted 1 year ago #

    in this section:

    foreach($catlisting as $catlist) {
            $i=0;

    move the $i=0; to the line before the foreach

    ----
    and try to change this line:

    echo $regions[$i] . " href=\"index.php?cat=" . $catlist->cat_ID . "\" title=\"" . $catlist->cat_name . "\">";

    to:

    echo $regions[$i] . " href=\"" . get_category_link($catlist->cat_ID) . "\" title=\"" . $catlist->cat_name . "\">";

    http://codex.wordpress.org/Function_Reference/get_category_link

  6. Jack Tummers
    Member
    Posted 1 year ago #

    Still doesn't work. I changed the way of filling the array so it is easier to follow, and your suggestion of the get_category_link. The only thing I get so far is the first link to the first child cat, but the echo of the #regio[$i] is skipped somehow.

    http://pastebin.com/1nFxfXvR

  7. alchymyth
    The Sweeper
    Posted 1 year ago #

    with your new way of filling the arry, you started with element 1;
    so your counter would need to start with 1: $i = 1;

    also, there are some excessive backslashes, for instance here:
    $regio[4]=\"<area sh....
    and at the same spot in all the array elements that follow;
    should read:
    $regio[4]="<area sh....

    http://pastebin.com/3PaGcYYb

  8. Jack Tummers
    Member
    Posted 1 year ago #

    Many thanks! I think it works now. I'm still learning php. so also thanks for the corrections in the syntax ;). One reason why it was not working is that I made all the categories allright, but some had no posts yet... :(

  9. Jack Tummers
    Member
    Posted 1 year ago #

    One solution causes the next problem/question... ;)

    In my situation I have two arrays. One for the regions of a an image map, which I fill in manually, the second one containing the child categories of the current category.

    The problem now is that the code above works, except when not all child categories have posts. If only the second of nine child categories already has posts, this child cat will be placed at region 1 because that's the first region in the array, $i = 1. So I need some kind of check to see if a child cat has posts, preferably by combining the two array into one new array, but I have no idea how to do that. Any suggestions?

  10. alchymyth
    The Sweeper
    Posted 1 year ago #

    try to use the 'hide_empty' parameter with the get_categories() code:

    http://codex.wordpress.org/Function_Reference/get_categories

    that should include the child categories, even if they have no posts(yet).

    $catlisting = get_categories('hide_empty=0&child_of=' . $cat_id);
  11. Jack Tummers
    Member
    Posted 1 year ago #

    That at least gives me back all the links at the right places :). In the end I want to have a rollover effect only for the regions that are already clickable. In fact these regions are countries.
    Now the only thing I need is to disable the links on the regions (= category link) that not yet have posts. I tried that with get_posts, but not good enough I'm afraid. Could you please have a look at the code as it is now?

    http://pastebin.com/JrzarmG7

Topic Closed

This topic has been closed to new replies.

About this Topic