Support » Fixing WordPress » How to display only one specific tag?

  • Resolved Alkorr

    (@alkorr)


    Hi. I have a post with multiple tags: sports + basket + NBA

    When displaying a post, I only want the tag ‘NBA’ to be displayed, not the all three tags.

    How can I do that?

    Thanks for your help!

Viewing 9 replies - 1 through 9 (of 9 total)
  • If you only want 1 tag to be displayed, why are you adding the other 2 tags?

    Thread Starter Alkorr

    (@alkorr)

    Hi esmi 🙂

    Because I only want one tag (the NBA (team) tag) to show on the front page, and on the single.php I want to display all the tags.

    I want to use a tag, not create a taxonomy. That’s why I have to find a way to only show a specific tag when it’s there.

    Thread Starter Alkorr

    (@alkorr)

    I’ve checked this page indeed, but when I add this code within my Query (where I want the tag to be displayed), I get a blank page…

    <?php
    if ($all_the_tags);
    $all_the_tags = get_the_tags();
    foreach($all_the_tags as $this_tag) {
    	if ($this_tag->name == "sometag" ) {
    ?>
    
    <p>SOME HTML CODE <img src="someimage.jpg"></p>
    
    <?php 	} else if ($this_tag->name == "someothertag" ) { ?>
    
    <p>SOME OTHER HTML CODE <img src="someotherimage.jpg"></p>
    
    <?php 	} else {
    		// it's neither, do nothing
    ?>
    		<!-- not tagged as one or the other -->
    <?
    	}
    }
    }
    ?>

    No idea why…

    Try removing if ($all_the_tags);

    Thread Starter Alkorr

    (@alkorr)

    I removed it, same result, blank page…

    Just so you know, my main loop is this one below (you helped me about it a while ago, it worked fine to avoid duplicates but now it doesn’t work anymore, I don’t why, but it’s another matter):

    <?php global $do_not_duplicate; ?>
    <?php $my_query = new WP_Query(array('posts_per_page' => 5, 'cat' => 1, 'post__not_in' => get_option( 'sticky_posts' )) );
    	while ($my_query->have_posts()) : $my_query->the_post();
    	$do_not_duplicate[] = $post->ID; ?>

    Then:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    
    <?php
    $all_the_tags = get_the_tags();
    foreach($all_the_tags as $this_tag) {
    	if ($this_tag->name == "sometag" ) {
    ?>
    
    <p>Name of my tag</p>
    
    <?php 	} else if ($this_tag->name == "someothertag" ) { ?>
    
    <p>Name of the other tag</p>
    
    <?php 	} else {
    		// it's neither, do nothing
    ?>
    		<!-- not tagged as one or the other -->
    <?
    	}
    }
    }
    ?>
    
    <?php $i++; ?>
    <?php endwhile;?>
    <?php wp_reset_query(); ?>

    And that doesn’t work, blank page. 🙂

    Hi Alkorr

    I have a similar question, but no solution. How can I show only a specific tag in single template?

    this is near to solution: http://wordpress.org/support/topic/how-to-get-post-tag

    Thread Starter Alkorr

    (@alkorr)

    Hi ddorr! I’ve found a way to do it within the loop, simple yet effective:

    <?php if(has_tag(‘sometag’)){ ?>Some tag<?php } ?>

    It works fine for me, hope it will help you! 🙂

    Thanks Alkorr. I will try your code.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to display only one specific tag?’ is closed to new replies.