WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display only one specific tag? (10 posts)

  1. Alkorr
    Member
    Posted 1 year ago #

    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!

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    If you only want 1 tag to be displayed, why are you adding the other 2 tags?

  3. Alkorr
    Member
    Posted 1 year ago #

    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.

  4. esmi
    Forum Moderator
    Posted 1 year ago #

  5. Alkorr
    Member
    Posted 1 year ago #

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

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    Try removing if ($all_the_tags);

  7. Alkorr
    Member
    Posted 1 year ago #

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

  8. ddorr
    Member
    Posted 1 year ago #

    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

  9. Alkorr
    Member
    Posted 1 year ago #

    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! :)

  10. ddorr
    Member
    Posted 1 month ago #

    Thanks Alkorr. I will try your code.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.