I need help with the following:

I have a taxonomy named location. I have a list of countries using that taxonomy.

I need to display the latest posts in a list with the flag and the post title.

I created a folder inside theme folder with all country flags. For instance Spain.png etc etc

How can I do a loop of posts and retrieve the location taxonomy associated to each post so I can put the url to flags folder + the taxonomy name + png?

cheers

Recommended Answers

All 21 Replies

Member Avatar for diafol

I'm assuming that you have DB tables. So maybe something like this:

POSTS

post_id
...
post_body
post_user_id

USERS

user_id
...
country_id

COUNTRIES

country_id
country_name
country_image

country_image = the filename of the flag of that particular country and possibly the path too. But I usually leave off the path in case I change the location - so maybe keep the path (directory) in a php config variable or even a CONSTANT.

So a query could be something like:

SELECT p.post_body, p.post_title, p.posted, u.username, c.country_image FROM posts AS p INNER JOIN users AS u ON p.post_user_id = u.user_id INNER JOIN countries AS c ON u.country_id = c.country_id

Although you could have each country with the image name the same as the country name, so avoiding the need to store this country_image field, it could be that your images do not have the country.png name. For example 'Dominican Republic' would probably be called dominican_republic.png - OK probably easy to fix with the odd string function, but needless complexity in my view.

Oh ok I forgot to mention this is in Wordpress! Sorry!

So I get to this code:

<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://localhost/expoff/wp-content/themes/inovve/images/flags/.png

ie the taxonomy name is missing.... :/

Member Avatar for diafol

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

Do a

print_r($terms[0]);

In the loop to see whether it's empty or not. In fact your 'alt' attribute is also empty, so it seems that your object isn't returning what it's supposed to.
If you had all this info, why didn't you show it in the first post? :)

OK will try that

I didn't have this info when started the thread, I tried hard to solve the problem after creating original post :)

It's empty.....

Why the hell $terms = get_the_terms( $recent->ID, 'location' ); doesn't get the taxonomy data?? I'm driving mad!

Member Avatar for diafol

I'm afraid I can't help you with user-defined functions to which I don't have access. I found this:

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

But not much help really. Check to see if you're retrieving the right ID in $recent['ID'] and I'm assuming 'location' is a valid taxonomy.

Are you sure it's $terms[0] and not just $terms ?

Member Avatar for diafol

Yes, but not native php function. I don't enjoy looking up codices for third party apps. It's WP's udf.

PS I edited my last post when you posted yours.

mate the problem is that on

$terms = get_the_terms( $recent->ID, 'location' );

$recent->ID returns null

but here http://codex.wordpress.org/Template_Tags/get_posts

says:

To access a post's ID or content without calling setup_postdata(), or in fact any post-specific data (data retained in the posts table), you can use $post->COLUMN, where COLUMN is the table column name for the data. So $post->ID holds the ID, $post->post_content the content, and so on. To display or print this data on your page use the PHP echo command, like so:

<?php echo $post->ID; ?>

Member Avatar for diafol

OK

So does $recent_posts contain anything? print_r this to see what's contained.

Does it have to be called $post instead of $recent_post ? Can't see why it should, but there again I'm no WP guy.

OK here's the problem:

this: $terms = get_the_terms( $recent->ID, 'location' );

has this:

array(1) {
[145]=>
object(stdClass)#103 (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 retrieve "name" field? I have

$flagimg='<img src="'.get_bloginfo('template_directory').'/images/flags/'.$terms[0]->name.'.png" alt="'.$terms[0]->name.'" border="0" />';

but not working....

Member Avatar for diafol

Is this the $terms object? Have you tried $terms->name. I really don't like std objects :(

Is it $terms[145]->name ?

Yes is $terms[145]->name !!!

But why $terms[145]???? I'm lost now....

OK 145 is the term ID on database

But this way I can achieve what I need....

Member Avatar for diafol

WHat is $recent->ID - is it 145??

If so, use this for now:

$terms[$recent->ID]->name

It's pretty horrible, but it should work for all different $recent->ID's

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 can see that

$id = $recent["ID"];
        $terms = get_the_terms( $id, 'location' );

is giving me the correct taxonomy (Portugal) in this strucuture

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″
}
}

this is an array. I don't know how to query this array to print Portugal name.... :(

Member Avatar for diafol
echo $terms[$recent->ID]->name;

Or can $recent_ID be an array?? If so you'll need to think of a different approach.

//EDIT

No just checked - it can't, so you should be OK

No because $recent->ID is the ID of the post...

I managed to get the flags doing:

<ul>
<?php
$args = array( 'numberposts' => '15' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$country = strip_tags( get_the_term_list( $wp_query->post->ID, 'location', '', ', ', '' ) );
        $flagimg='<img src="'.get_bloginfo('template_directory').'/images/flags/'.$country.'.png" alt="'.$country.'" border="0" />';
        echo '<li class="country-'.$country.'"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >'.$flagimg.' '.$recent["post_title"].'</a> </li> ';
}
?>
</ul>

buuut..... the flag for 1st post is repeated on others....

see

http://inovve.net/expoff/

Member Avatar for diafol

seems ok to me. what's wrong - I see spain, france, portugal and a few unfound items.

I finally did it

<?php
$args = array( 'numberposts' => '15' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$country = strip_tags( get_the_term_list( $recent->ID, 'location', '', ', ', '' ) );
        $flagimg='<img src="'.get_bloginfo('template_directory').'/images/flags/'.$country.'.png" alt="'.$country.'" border="0" width="18px" />';
        echo '<li class="country-'.$country.'"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >'.$flagimg.' '.$recent["post_title"].'</a> </li> ';
}
?>
</ul>

Thank you so much for your help mate ;)

Member Avatar for diafol

dim problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.