Jason Mutton
Jason Mutton

Reputation: 15

Wordpress Taxonomies

I'm about ready to cry!

I've done a lot of Google searches but I can't quite get this piece of coding working the way I want it to.

In Wordpress I have the following taxonomy:

Active
- Open
- In-Progress
- Awaiting Parts
- Pending / On-Hold
- Awaiting Pick-up

Closed

https://dl.dropboxusercontent.com/u/30177707/wo-tax.png

What I would like is for the child to be displayed for the specific post and if there are no children I would like to display just the parent.

Heres a screenshot I've edited so it gives a better picture of what I'm asking. https://dl.dropboxusercontent.com/u/30177707/stackoverflow.png

This is the code I've been playing with:

$terms = wp_get_post_terms($post->ID, 'pctracker_workorderstatus');
$count = count($terms);
if ( $count > 0 ){
   foreach ( $terms as $term ) {
      echo $term->name .'<br>';
   }
}

At present its displaying the parent and child for the post.

Would be very grateful for some help or direction!

Thanks, Jase

Upvotes: 0

Views: 162

Answers (1)

Philippe Boissonneault
Philippe Boissonneault

Reputation: 3949

You'll have to edit column content like this :

This is an example code you could adapt to your needs, basically I look at all terms to get which ones are parents and childs. Then depending of the results I display parents or childs. In your case, there will always be 1 parent and/or 1 child. But the code should work. (not tested)

function MYCUSTOMPOSTTYPE_custom_columns( $column_name, $id ) {
    switch ( $column_name ) {
        case 'status':
                $terms = wp_get_post_terms($id, 'pctracker_workorderstatus');
                $count = count($terms);
                if ( $count > 0 ) {
                    $parents = array();
                    $childs = array();
                    foreach ( $terms as $term ) {
                        if(!empty($term->parent)) {
                            $childs[] = $term;
                        } else {
                            $parents[] = $term;
                        }
                    }
                    //display parent if there no child
                    if(empty($childs)) {
                        foreach($parents as $p) {
                            echo $p->name;
                        }
                    } elseif(!empty($parents) && !empty($childs)) {
                        //don't display parent
                        foreach($childs as $p) {
                            echo $p->name;
                        }
                    }                        
                 }
            break;

        default:
            break;
    } // end switch
}
add_action( 'manage_MYCUSTOMPOSTTYPE_posts_custom_column', 'MYCUSTOMPOSTTYPE_custom_columns', 10, 2 );

Upvotes: 1

Related Questions