0

What i am trying to do is echo the last hierarchial child term for the taxonomy. My taxonomy is propertytype.

My hierarchial structure is:

  • Real Estate (Parent)
  • Commercial (Child)
  • Offices (Child of Commercial)
  • Basement (Child of Commercial)

So in the above case i would only like to echo Offices, or which ever the last hierarchial child term is.

I thought my code below would do this, but it actually echoes Commercial because Commercial is alphabetically before Offices. Now Basement will be ok because it is alphabetically before Commercial.

How do I always echo the last hierarchial child term only?

My code is:

$terms = get_the_terms($wp_query->post->ID, 'propertytype');                    
//die(print_r($terms));
$props = array();
foreach ($terms as $term) { 
    $hasChildrenTest = get_term_children($term->ID, 'propertytype' );
    if ($term->parent) {
        if (empty($hasChildrenTest) && !is_wp_error($hasChildrenTest)) {
            $props[] = $term->slug;
        }
    }
}
echo $props[0];

1 Answer 1

0

With wp_get_object_terms() you're able to manipulate the sorting.

Make sure you tweak the sorting to what you want.

Try something more or less like this (Updated: 5/17/2012 -- 8:15AM):

$taxonomyName = 'producttype';
$terms = wp_get_object_terms($wp_query->post->ID, $taxonomyName, array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'));
if(!empty($terms)){
    if(!is_wp_error($terms)){
        for($i = 0; $i < count($terms); $i++){
            if($terms[$i]->parent != 0){
                $termChildren[] = $terms[$i];
            }
        }
    }
}
print_r($termChildren);

Output a list of all public custom taxonomies

<?php 
$args=array(
  'public'   => true,
  '_builtin' => false

); 
$output = 'names'; // or objects
$operator = 'and'; // 'and' or 'or'
$taxonomies=get_taxonomies($args,$output,$operator); 
if  ($taxonomies) {
  foreach ($taxonomies  as $taxonomy ) {
    echo '<p>'. $taxonomy. '</p>';
  }
}
?>

Output a list of all public custom post types

<?php 
$args=array(
  'public'   => true,
  '_builtin' => false
); 
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$post_types=get_post_types($args,$output,$operator); 
  foreach ($post_types  as $post_type ) {
    echo '<p>'. $post_type. '</p>';
  }
?>
7
  • Hi There, Thanks for the response. Unfortunately i get the same results with this as i do with my code where is does not seperate the child and child of a child. Just in case I am missing something, can i display a child of a child? the above code seems to exclude the parent (which is correct) but then echos the first alphabetical child term associated with the post. So in my above example, because COMMERCIAL is already a child, and OFFICES is a child of COMMERCIAL, it displays COMMERCAL. No matter what i try, I can't echo just OFFICES : ( Any ideas? Thanks in advance
    – PaulB
    Commented May 17, 2012 at 7:22
  • @PaulB made some revisions. Try this. Commented May 17, 2012 at 13:16
  • hi michael, tried the code but it gives a php error : (
    – PaulB
    Commented May 20, 2012 at 19:27
  • @PaulB replace where it says 1029 with $wp_query->post->ID, i updated the code again Commented May 20, 2012 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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