WordPress.org

Ready to get started?Download WordPress

Forums

Trying to output a post's terms (taxonomy) as text, not URLs (24 posts)

  1. widecast
    Member
    Posted 4 years ago #

    I'm looking for something similar to get_the_term_list but in a way that doesn't output the results as URLs.

    I would just like the actual term names for a specific taxonomy that are associated with the post.

    I'm all ears!

  2. whooami
    Member
    Posted 4 years ago #

  3. widecast
    Member
    Posted 4 years ago #

    Hi there, thx for the quick answer.

    That's definitely the right direction. I tried using the following:

    <?php echo get_the_terms($post->ID, 'type'); ?>

    but all that is echoed is the word 'array'. How do I print the results of this array? I'm new to PHP and even newer to arrays. :)

    David

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    I think valid values are..
    category
    post_tag
    and
    link_category

    I don't think type will give you any results...

    NOTE: to print an array, use print_r($yourarray); that's what print_r is for... ;)

  5. widecast
    Member
    Posted 4 years ago #

    According to the first link, where I used the word 'terms' is in reference to my taxonomy title.

    I now tried:

    $terms = get_the_terms($post->ID, 'cuisine_type');  
    
    print_r($terms);

    Now I get the output:

    Array ( [7] => stdClass Object ( [term_id] => 7 [name] => Mexican [slug] => mexican [term_group] => 0 [term_taxonomy_id] => 7 [taxonomy] => cuisine_type [description] => This is the Mexican food section [parent] => 0 [count] => 2 [object_id] => 6 ) )

    The 'type' in this case is 'Mexican'. That's what I'd like to print.

  6. widecast
    Member
    Posted 4 years ago #

    Can anyone tell me if I'm on the right track? How can I echo just the 'Mexican' term in the array? I've been trying to accomplish this for hours with no luck. Any help is appreciated!

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    When you're talking about terms, are you actually dealing with categories?...

    I say this because i'm still fairly new (this year anyway) to WordPress and whenever i see the mention of "terms" it just confuses me a little because i'm use to referring to categories..

    Perhaps if you explain what it is you're attempting to do someone can suggest an easier approach...

    There's bundles of category functions, who knows there may be one suited to what you want to do...

    Or am i losing the plot, and totally off the mark, lol...

  8. widecast
    Member
    Posted 4 years ago #

    Thanks for the reply. Actually I'm talking about a new feature in 2.8 called taxonomy. It works a bit different than tags and categories.

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

    This function works like a charm to list my taxonomy terms as links but I'd like to use those same terms as normal text within the WordPress post templates as well.

    If you'd like to learn about it, check the functions here: http://codex.wordpress.org/Function_Reference (find 'taxonomy').

    The article that got me into taxonomy is this one, check it out as well if you're interested: http://justintadlock.com/archives/2009/06/04/using-custom-taxonomies-to-create-a-movie-database

    Thanks for trying to help. If you can help more, please do, if not, enjoy the reading!

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    I'll have a good read when i can spare more time then to have a brief skim ... in the mean time perhaps you might be fortunate enough to catch Justin, he does frequent the forum..

    Or perhaps whooami can advise further, because i'd only be guessing or making assumptions at this point, and that may not be of much help to you..

    Generally when you want to grab data from an array you'd use some form of loop, to loop over each item in the array unless you're selecting a particular item from the given array...

    If i knew something more definitive about taxonomies i'd give you a more useful answer to work with... :)

    I'll be watching the thread in any case.... :)

  10. Adz
    Member
    Posted 4 years ago #

    Try this:
    <?php echo $terms['name']; ?>
    That specifies exactly which index you want from the array.

  11. Adz
    Member
    Posted 4 years ago #

    I was trying to output all of the terms from a taxonomy and ended up with something like this. I made a few changes to suit your situation. Though I haven't tried it, I'd hope it works.

    <?php
    //Your code:
    $terms = get_the_terms($post->ID, 'cuisine_type');
    print_r($terms);
    
    //my code
    foreach ($terms as $taxindex => $taxitem) {
      if ($taxindex=='name') {
    ?>
     <span><?php echo $taxitem; ?></span>
    <?
      } //end if
    } // end foreach
    ?>

    In theory, your code would give you the array you posted earlier. Then my code would loop through the array and for each index that matches 'name' (which is where "Mexican" would be stored) will echo the value.

    Hope that helps.

  12. Mark / t31os
    Moderator
    Posted 4 years ago #

    Adz, the result is an array, each item's value is an object..

    Something like this should work though...

    <?php
    // Get terms for post
    $terms = get_the_terms( $post->ID , 'cuisine_type' );
    
    // Loop over each item since it's an array
    foreach( $terms as $term ) {
    	// Print the name method from $term which is an OBJECT
    	print $term->name . '<br />';
    	// Get rid of the other data stored in the object, since it's not needed
    	unset($term);
    }
    ?>

    Very basic example, but referencing data in an object is a little different to that in array.

    You can test the above by changing cuisine_type to post_tag if your posts have tags (rather then having to create custom taxonomies and terms just to test it).

  13. widecast
    Member
    Posted 4 years ago #

    Thanks for the replies! I found a pretty simple way that I hadn't thought of... Basically using get_the_term_list and then using strip_tags to get just the terms as text.

    Works like a charm. Hopefully this helps someone in the future.

    Example:

    $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'taxonomyyouwanttoquery', '', ', ', '' ) );
    echo $terms_as_text;

    Just replace the taxonomyyouwanttoquery with the taxonomy you'd like to check against for terms associated to the post.

  14. Mark / t31os
    Moderator
    Posted 4 years ago #

    I looked at get_the_term_list several times, and still didn't think to use that because it generated links, can't believe i didn't consider strip_tags .... I over-thought this one...

    Very easy way to handle it... :)

  15. design_dolphin
    Member
    Posted 4 years ago #

    @widecast, @31os_, @Adz,
    Unfortunately I couldn't get the examples of code to work off the bat (as warned might be the case).

    This did work:

    <?php
    
    $terms_as_text = get_the_term_list( $post->ID, 'custom taxonomy name', '', ', ', '' ) ;
    echo strip_tags($terms_as_text);
    
    ?>

    And this works as well:

    <?php
    
    $terms = get_the_terms($post->ID, 'custom taxonomy name');
    print_r($terms);
    
    echo '<ul>';
    
    foreach ($terms as $taxindex => $taxitem) {
    
    echo '<li>' . $taxitem->name . '</li>';
    
    }
    
    echo '</ul>'
    
    ?>

    My goal was to show the terms of a post from a custom taxonomy in the sidebar as an unordened list.

    Thanx for sharing the code. :-)

  16. design_dolphin
    Member
    Posted 4 years ago #

    I had posted this previously, but noticed a HTML error at the last minute.

    If you want to use the get_the_term_list() for an unordened list, I encountered the following in order to make it work. Otherwise the first record is left without an li tag.

    <?php
    
    echo '<ul>';
    
    $terms_of_post = get_the_term_list( $post->ID, 'custom taxonomy name', '<li>','<li>', '</li>', '' );
    echo $terms_of_post;
    
    echo '</ul>';
    
    ?>

    One would expect:

    <?php
    
    $terms_of_post = get_the_term_list( $post->ID, 'custom taxonomy name', '<ul>','<li>', '</li>', '</ul>' );
    echo $terms_of_post; 
    
    ?>

    to work.

    Subject to review. Can anyone confirm this? Or maybe we should clear up the Codex page on get_the_term_list().

  17. Mark / t31os
    Moderator
    Posted 4 years ago #

    I'd expect ..

    <ul>
    <?php get_the_term_list( $post->ID, 'taxonomy' , '<li>' , '</li><li>' , '</li>' ); ?>
    </ul>

    ..to work... ;)

    Codex page is clear enough i think..

  18. Nneon
    Member
    Posted 3 years ago #

    <ul>
    <?php get_the_term_list( $post->ID, 'taxonomy' , '<li>' , '</li><li>' , '</li>' ); ?>
    </ul>

    How would you go about adding a class name that is the same as the taxonomy name, to each of those list items?

  19. Mark / t31os
    Moderator
    Posted 3 years ago #

    Add it to the element in the code literally?

    <ul>
    <?php get_the_term_list( $post->ID, 'my_taxonomy' , '<li class="my_taxonomy">' , '</li><li class="my_taxonomy">' , '</li>' ); ?>
    </ul>
  20. Nneon
    Member
    Posted 3 years ago #

    Thanks for the speedy reply, not quite what i was after. I'm trying to create a class name for the value of each taxonomy.

    <ul>
    <?php get_the_term_list( $post->ID, 'colours' , '<li>' , '</li><li>' , '</li>' ); ?>
    </ul>

    Produces a list something like:

    • red
    • blue
    • green

    I want each list item to have a class that corresponds to the list value... eg red, blue or green

  21. Mark / t31os
    Moderator
    Posted 3 years ago #

    Oh, you mean the term, beyond the scope of that particular function, the example i gave further up using get_terms would be suitable though.

    Something like..

    <?php
    $taxonomy = 'my_taxonomy';
    $terms = get_the_terms( $post->ID , $taxonomy );
    
    if ( empty( $terms ) ) :
    
    echo '<ul>';
    
    foreach ( $terms as $term ) {
    	$link = get_term_link( $term, $taxonomy );
    	if ( !is_wp_error( $link ) )
    		echo '<li class="' . $term->slug. '"><a href="' . $link . '" rel="tag">' . $term->name . '</a></li>';
    }
    
    echo '</ul>';
    
    endif;
    ?>

    Untested, but it should work..

  22. Nneon
    Member
    Posted 3 years ago #

    Ah perfect! Well almost, you were missing the ! in:

    if ( !empty( $terms ) ) :

    Thanks heaps, exactly what I was after ;)

  23. Mark / t31os
    Moderator
    Posted 3 years ago #

    Oops... :)

    Happy to help.. ;)

  24. vanwidp
    Member
    Posted 3 years ago #

    I am trying to do something like this. Instead of returning a list of all the categories within a custom taxonomy, I only want to return the first one. I have a portfolio page with 'media' as my custom taxonomy. I want to show the name of each category inside 'media' (identity, print, web) as a link to a category archive. Underneath the link will be thumbnails of the posts in that category.

    When I use the get_term_link inside the loop, it returns the link each time it shows a post. If I use it in its own seperate loop it returns the link like I want, but it returns it multiple times depending on the number of posts.

    <?php
    $post_type = 'Portfolio';
    $tax = 'media';
    $tax_terms = get_terms($tax);
    if ($tax_terms) {
    foreach ($tax_terms as $tax_term) {
    $args=array(
    'post_type' => $post_type,
    "$tax" => $tax_term->slug,
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'caller_get_posts'=> 1
    );
    
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    
    $divName = $tax_term->name ;?>
    
    <div class="<?php echo $divName; ?>">
    
    <?php echo $tax_term->name. ' Projects ' ;

    This is what I'm using now to display the category name a single time. Is it possible to attach the link to this? Can get_term_link be used outside the loop, or modified to return only the first result?

Topic Closed

This topic has been closed to new replies.

About this Topic