• Resolved cipriano200

    (@cipriano200)


    The code below pulls child categories of a parent, works perfectly.

    Still i want to use this type of solution for TAXONOMIES, any ideas how to convert this?

    <?php
    global $ancestor;
    $childcats = get_categories('child_of=' . $cat . '&hide_empty=1');
    foreach ($childcats as $childcat) {
      if (cat_is_ancestor_of($ancestor, $childcat->cat_ID) == false){
        echo '<a class="trick product-search-item" href="'.get_category_link($childcat->cat_ID).'">';
    
    	echo '<img src=';
    	echo z_taxonomy_image_url($childcat->term_id);
    	echo '>';
    	echo '<div class="icon"></div>';
    	echo '<span>';
    	echo $childcat->cat_name . '</span>';
        echo '</a>';
        $ancestor = $childcat->cat_ID;
      }
    }
    ?>
Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator bcworkz

    (@bcworkz)

    All the category functions have generic term equivalents, such as get_terms(), term_is_ancestor_of(), get_term_link() etc. You need to do more than rename functions because the generic term functions all take more parameters, namely the taxonomy the term is in. There could be other differences, so check the Codex page for every function you convert.

    Thread Starter cipriano200

    (@cipriano200)

    Already tried that, maybe i am missing something?

    The converted code:

    <?php
    global $ancestor;
    $childcats = get_terms('child_of=' . $cat . '&hide_empty=1');
    foreach ($childcats as $childcat) {
      if (term_is_ancestor_of($ancestor, $childcat->cat_ID) == false){
        echo '<a class="trick product-search-item" href="'.get_term_link($childcat->cat_ID).'">';
    
    	echo '<img src=';
    	echo z_taxonomy_image_url($childcat->term_id);
    	echo '>';
    	echo '<div class="icon"></div>';
    	echo '<span>';
    	echo $childcat->cat_name . '</span>';
        echo '</a>';
        $ancestor = $childcat->cat_ID;
      }
    }
    ?>

    Shows this error:

    Warning: Missing argument 3 for term_is_ancestor_of(), called in URL on line 23 and defined in D:\zEasyPHP-DevServer\data\localweb\decor\wp-includes\taxonomy.php on line 1542
    
    Catchable fatal error: Object of class WP_Error could not be converted to string in D:\zEasyPHP-DevServer\data\localweb\decor\wp-content\themes\dd\taxonomy-parchet-cats.php on line 24
    Moderator bcworkz

    (@bcworkz)

    You need to specify the taxonomy to which the terms belong as the missing 3rd argument.

    The second error is because you have no error checking code for the return of a function, so you are probably trying to echo out a WP_Error object. Ideally you should insert error handling script, but eliminating the cause of the error could suffice if all input is managed such that another error is highly unlikely.

    Thread Starter cipriano200

    (@cipriano200)

    tried this but failed:

    <?php
    global $ancestor;
    $childcats = get_terms('parchet-cats', 'child_of=' . $cat . '&hide_empty=1');
    foreach ($childcats as $childcat) {
      if (cat_is_ancestor_of($ancestor, $childcat->term_id) == false){
        echo '<a class="trick product-search-item" href="'.get_term_link($childcat->term_id, 'parchet-cats').'">';
    
        echo '<img src=';
        echo z_taxonomy_image_url($childcat->term_id);
        echo '>';
        echo '<div class="icon"></div>';
        echo '<span>';
        echo $childcat->term_name . '</span>';
        echo '</a>';
        $ancestor = $childcat->term_id;
      }
    }
    ?>

    Shows this error:

    Catchable fatal error: Object of class WP_Error could not be converted to string in D:\zEasyPHP-DevServer\data\localweb\decor\wp-content\themes\dd\taxonomy-parchet-cats.php on line 24

    Can you enlight me where i wrong?

    Thread Starter cipriano200

    (@cipriano200)

    LOL i’m not good at this

    ok I had updated your code, try it and tell if it works or not:

    <?php
    global $ancestor;
    $childcats = get_terms('parchet-cats', 'child_of=' . $cat . '&hide_empty=1');
    foreach ($childcats as $childcat) {
      if (cat_is_ancestor_of($ancestor, $childcat->term_id) == false){
        echo '<a class="trick product-search-item" href="'.get_term_link($childcat->slug, 'parchet-cats').'">';
    
        echo '<img src=';
        echo z_taxonomy_image_url($childcat->term_id);
        echo '>';
        echo '<div class="icon"></div>';
        echo '<span>';
        echo $childcat->name . '</span>';
    
        echo '</a>';
        $ancestor = $childcat->term_id;
      }
    }
    ?>

    Thread Starter cipriano200

    (@cipriano200)

    closer, but shows all categories, not child of parent

    here: https://s14.postimg.org/ixy27qtsx/pic1.png
    and here: https://s14.postimg.org/kyolskpy9/pic2.png

    Thread Starter cipriano200

    (@cipriano200)

    trying final code

    Thread Starter cipriano200

    (@cipriano200)

    THIS CODE WORKED

    <?php
    global $ancestor;
    $cat = $wp_query->queried_object->term_id;
    $childcats = get_terms('parchet-cats', 'parent=' . $cat . '&hide_empty=1');
    foreach ($childcats as $childcat) {
      if (cat_is_ancestor_of($ancestor, $childcat->term_id) == false){
        echo '<a class="trick product-search-item" href="'.get_term_link($childcat->slug, 'parchet-cats').'">';
    
        echo '<img src=';
        echo z_taxonomy_image_url($childcat->term_id);
        echo '>';
        echo '<div class="icon"></div>';
        echo '<span>';
        echo $childcat->name . '</span>';
        echo '</a>';
        $ancestor = $childcat->term_id;
      }
    }
    ?>

    how to display category parents only of an taxonomy in archive-{taxonomy-slug}.php

    ?

    Moderator bcworkz

    (@bcworkz)

    @christedor – I’m not sure what you are actually asking for. Display posts belonging to category parent or just show the category parent as a label, or as a link to the corresponding archive? What of the current category posts? Still displayed? Mixed in with parent posts? Only show parent posts? There’s many possibilities, you’ll need to be very specific.

    FYI, adding on to existing topics is not allowed in these forums. Please start a new topic and fully describe what you are trying to accomplish.

    @bcworkz

    I have a custom post type named, “Countries”
    Taxonomy is called “Countries-categories”

    I want to display only parent categories of taxonomy “countries-categories” in my archive-countries.php

    So my archive page will look like this:
    Europe
    -country1
    -country2
    Asia
    -country1
    -country2
    Africa
    -country1
    -country2

    ETC..

    Then after the that taxonomy-countries-categories.php will do its thing with the last code “cipriano200” posted, also that code displays image for category, with CATEGORIES IMAGES PLUGIN, so i want that to.

    Sorry if i broke the rules, but this topic is close in what i need.

    i solved the problem somehow

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘how to convert this code?’ is closed to new replies.