WordPress.org

Ready to get started?Download WordPress

Forums

How to display taxanomy field from latest posts (4 posts)

  1. miguel84
    Member
    Posted 1 year ago #

    Hi

    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 ) );
    }

    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"

    I don't know how to do it... can anyone give me a hand to jump start me please?

  2. miguel84
    Member
    Posted 1 year ago #

    ok 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>

    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.... :/

  3. miguel84
    Member
    Posted 1 year ago #

    So my $terms[0]->slug is empty...

    Any idea why???

  4. miguel84
    Member
    Posted 1 year ago #

    $recent->ID returns null value.... But it shouldn't...

Topic Closed

This topic has been closed to new replies.

About this Topic