0

I'm searching for a way to display the current taxonomy slug in a post.

Edited to make it more clearer here a more accurate example.

I have the CPT activites. I have more than 200 article posts about activities, and I want to categorize these into different areas (taxonomy) and different kind of activites. To get more specific, here my example:

Activities (Custom Post Type)
 - running (Taxonomy)
 - watersports (Taxonomy)
   - swimming (Term)
     - article post about swimming 1 (Post)
     - article post about swimming 2 (Post)
     - article post about swimming 3 (Post)
     - article post about swimming 4 (Post)
   - diving (Term)
   - boating (Term)
   - Kiteboating (Term)
 - materials arts (Taxonomy)
 - teamsports (Taxonomy)

If I'm in one of these posts (post about swimming), I want to display the current taxonomy (watersports) inside the article.

I have found a way to get display the current term (Term 1) but unfortunally I need the slug of the active taxonomy slug inside the post. The slug of this taxonomy is finally important, to display a list of more activies beside watersports. I'd try it like this way:

   <h4>More activities beside "watersport":</h4>
    <?php 
    $taxonomy     = 'HERE THE SLUG OF CURRENT TAXONOMY';
    $orderby      = 'name'; 
    $show_count   = 0;  
    $pad_counts   = 0;    
    $hierarchical = 1;    
    $title        = '';

    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title
    );
    ?>
    <ul>
    <?php wp_list_categories( $args ); ?>
    </ul>

Thanks for your help!

  • Are you able to put some real names into your example? If we could see what data you are working with then we might be able to suggest a structure that would work for you. – Andy Macaulay-Brook Aug 4 '16 at 21:15
0

There isn't a concept of a post mapping to a single taxonomy, so this won't work out.

Taxonomies are things like colour and size with terms such as lilac, azure, maroon, small, medium and large.

So your question is a bit like asking to be able to display "colour" when on a post, whereas the real mappings may well be to blue for colour and medium for size.

Are you able to put some real names into your example? If we could see what data you are working with then we might be able to suggest a structure that would work for you.

Edit: Expanded answer based on clearer information from OP.

You're misunderstanding slightly how taxonomies, terms and posts are structured. The taxonomy holds a set of terms which are like labels to apply to your posts. Your posts don't sit within taxonomies and terms like files within folders, rather the terms tag the posts. Terms within a taxonomy can be hierarchical and I think this is what you need.

Create one taxonomy called something like "type of activity." Make it hierarchical so that you can then add nested terms like this:

 - running (Term)
 - watersports (Term)
   - swimming (Term)
   - diving (Term)
   - boating (Term)
   - Kiteboating (Term)
 - materials arts (Term)
 - teamsports (Term)

Then instead of trying display the name of a taxonomy, you want to display the name of the top level term. You can get a list of just the top level terms using:

$args = array(
    'taxonomy' => 'activity_type',
    'parent' => 0,
    'hide_empty' => false           
);
$terms = get_terms( $args );

You can get all the top level terms that apply to the current post like this:

$args = array(
    'parent' => 0,          
);  
$terms = wp_get_object_terms( $postID, 'activity_type', $args );
  • Perfect! The way you to create terms in a herachical structure makes now more sense to me. I'll try this way and give you a feedback. Thanks so much! – Filip Aug 5 '16 at 13:51
  • No problem! Glad to help. – Andy Macaulay-Brook Aug 5 '16 at 13:56
0

thanks for your answer. Sure, to make it more clearer here a more accurate example.

I have the CPT activites. I have more than 200 article posts about activities, and I want to categorize these into different areas (taxonomy) and different kind of activites. To get more specific, here my example:

Activities (Custom Post Type)
 - running (Taxonomy)
 - watersports (Taxonomy)
   - swimming (Term)
     - article post about swimming 1 (Post)
     - article post about swimming 2 (Post)
     - article post about swimming 3 (Post)
     - article post about swimming 4 (Post)
   - diving (Term)
   - boating (Term)
   - Kiteboating (Term)
 - materials arts (Taxonomy)
 - teamsports (Taxonomy)

If I'm in one of these posts (post about swimming), I want to display the current taxonomy (watersports) inside the article.

The slug of this taxonomy is finally important, to display a list of more activies beside watersports. I'd try it like this way:

   <h4>More activities beside "watersport":</h4>
    <?php 
    $taxonomy     = 'HERE THE SLUG OF CURRENT TAXONOMY';
    $orderby      = 'name'; 
    $show_count   = 0;  
    $pad_counts   = 0;    
    $hierarchical = 1;    
    $title        = '';

    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title
    );
    ?>
    <ul>
    <?php wp_list_categories( $args ); ?>
    </ul>

Thanks for your help!

  • Thanks - I've copied your update back into an edit of the question to keep things tidy. I'll update my answer to suit. – Andy Macaulay-Brook Aug 5 '16 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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