Wordpress - Get the taxonomy associated to a post

Discussion in 'PHP' started by miguel84, Jun 26, 2013.

  1. #1
    I need to display an icon associated to a custom Wordpress taxonomy. The code for the taxonomy is:

    $labels = array(
        'name' =>  'Country/State/City' ,
        'singular_name' =>  'Genre' ,
        'search_items' =>  __( 'Search Country/State/City' ),
        'all_items' => __( 'All Country/State/City' ),
        'parent_item' => __( 'Parent Country/State/City' ),
        'parent_item_colon' => __( 'Parent Genre:' ),
        'edit_item' => __( 'Edit Country/State/City' ),
        'update_item' => __( 'Update Country/State/City' ),
        'add_new_item' => __( 'Add New Country/State/City' ),
        'new_item_name' => __( 'New Country/State/City' ),
        'menu_name' => __( 'Country/State/City' ),    );
        register_taxonomy( 'location', 'post', array( 'hierarchical' => true, 'labels' => $labels, 'query_var' => true, 'rewrite' => true ) );
        }  
    PHP:
    I created a folder inside my theme folder which contains the country flags (Spain.png, France.png, etc)
    I need to display a list with the latest posts titles with the flag next to it.
    So I thought I could do the loop and get, let's say, the latest 10 posts from a post category, display it's title and the icon next to it would be the "path-to-image-folder/taxonomy-country.png"
    So I got to this:
    <h2>Recent Posts</h2>
    <ul>
    <?php
    $args = array( 'numberposts' => '15' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
            $terms = get_the_terms( $recent->ID, 'location' );
            $flagimg='<img src="'.get_bloginfo('template_directory').'/images/flags/'.$terms[0]->slug.'.png" alt="'.$terms[0]->name.'" border="0" />';
            echo '<li class="country-'.$terms[0]->slug.'"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >'.$flagimg.' '.$recent["post_title"].'</a> </li> ';
    }
    ?>
    </ul>
    PHP:
    but the img src link i get is: http://inovve.net/expoff/wp-content/themes/inovve/images/flags/.png
    ie the taxonomy name is missing.... :/



     
    miguel84, Jun 26, 2013 IP
  2. miguel84

    miguel84 Active Member

    Messages:
    70
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    88
    #2
    So my $terms[0]->slug is empty...

    Any idea why???
     
    miguel84, Jun 27, 2013 IP
  3. miguel84

    miguel84 Active Member

    Messages:
    70
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    88
    #3
    $recent->ID returns null value.... But it shouldn't...
     
    miguel84, Jun 27, 2013 IP
  4. miguel84

    miguel84 Active Member

    Messages:
    70
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    88
    #4
    what I need is I have a post, that post has a taxonomy, I query the last 5 posts, for each post I need to retrieve the taxonomy name...
    I inside the loop
    $id = $recent["ID"]; $terms = get_the_terms( $id, 'location' );
    I then do
    var_dump($terms);
    and I get
    array(1) { [145]=> object(stdClass)#104 (10) { ["term_id"]=> string(3) “145″ ["name"]=> string(8) “Portugal” ["slug"]=> string(8) “portugal” ["term_group"]=> string(1) “0″ ["term_taxonomy_id"]=> string(3) “146″ ["taxonomy"]=> string(8) “location” ["description"]=> string(0) “” ["parent"]=> string(2) “95″ ["count"]=> string(1) “2″ ["object_id"]=> string(3) “238″ } }
    How do I echo 'Portugal'?
     
    miguel84, Jun 27, 2013 IP