here is my first question here !

I learn a lot reading question an answers here, but I still have a lot more to learn...

So, I want to create a simple shortcode, to return an image "If this taxonomy"

I think I am missing something obvious with the "Qwery"

Here's my code :


function fun_set_taxo_image($post_id) {

    if ( have_posts() ) {

        while ( have_posts() ) {


            if( has_term($post_id, 'term-name_a', 'my_taxo' ) ) {

                return "The image A";

            } elseif ( has_term($post_id, 'term-name_b', 'my_taxo' ) ) {

                return "The image B";




add_shortcode('taxo_image', 'fun_set_taxo_image');


I want to insert the shortcode in a grid post based, to identify each post with a little image depending of taxonomy... When I insert other shortcodes like [acf field="field_name" post_id="%post_id%"] is work well, the shortcode return any data related with the post...

So, hope somebody can helpmeet with this...

Thanks guys

  • Yo! @cybmeta thanks for the edit ! Sorry, I try to envelope my code a few times, pero nunca lo logré...!!! – MicoLoco Mar 17 '15 at 1:01

There's no need to replicate The Loop in your plugin; WordPress is working within a post when it processes a shortcode, so the post ID is available when the shortcode is being processed.

Also, your arguments for has_term are out of order. And finally, it's a best practice to make a shortcode all one word.

function fun_set_taxo_image( $atts, $content = null ) {
    global $post;

    if( has_term( 'term_name_a', 'my_taxo', $post->ID ) ) {
        return "image A";
    else {
        return "image B";

add_shortcode( 'taxoimage', 'fun_set_taxo_image' );
| improve this answer | |
  • Hi Doug, thanks a lot for your answer and advices. I test your code as you wrote here, and is not working (of course, I change the "term_names") but is always returning "image B" even if has "term_name_a"... I think the solution is not so far... What do you think about?? – MicoLoco Mar 17 '15 at 0:55
  • @MicoLoco I think the problem is the scope of the post itself within the function. I have updated my code, see if this works. – user12479 Mar 17 '15 at 13:47
  • Thanks again dude, but that still always returning "Image B"... I try too global $wp_post_types; $obj = $wp_post_types['movie']; cause I'm working with PostType... Important info: when I insert the shortcode [taxoimage] in a post, is work fine ! But when I insert in the a post grid item (there where I need) is always returning "Image B". I think the function is scoping the main page and not the post item ;( – MicoLoco Mar 17 '15 at 14:49
  • @MicoLoco My last try is this edited answer. – user12479 Mar 17 '15 at 16:47
  • Thanks for help and all your try, but is still not working... I'll take more time to see how I can make this work. Cheers Dude! – MicoLoco Mar 17 '15 at 17:40

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.