• Hi,

    I am trying to use

    <?php if (has_tag('keyword')) { ?>

    or

    <?php if (is_tag('keyword')) { ?>

    to display content for certain posts with that particular tag but it does not work.

    If I replace “is_tag” or “has_tag” with “is_single” and put the post ID, it works.

    Please help, thank you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • <?php if (has_tag('keyword')) { ?> is the correct function but it needs to go inside the wordpress loop. Like so:

    <?php while (have_posts()) : the_post(); ?>
    <?php if (has_tag('keyword')) { ?>
    	<div class="post">
    		<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
    		<small><?php the_time('l, F jS, Y') ?></small>
    		<div class="entry">
    			<?php the_content() ?>
    		</div>
    	</div>
    <?php }else{ ?>
    something else
    <?php } ?>
    <?php endwhile; ?>

    Thread Starter invisiontech

    (@invisiontech)

    Tomontoast,

    Thank you, works great.

    Can I expand that and do this?

    <?php while (have_posts()) : the_post(); ?>
    <?php if (has_tag('keyword')) { ?>
    	<div class="post">
    		<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
    		<small><?php the_time('l, F jS, Y') ?></small>
    		<div class="entry">
    			<?php the_content() ?>
    		</div>
    	</div>
    <?php }else{ ?>
    <?php if (has_tag('keyword-2')) { ?>
    something else
    <?php }else{ ?>
    <?php if (has_tag('keyword-3')) { ?>
    something else
    <?php }else{ ?>
    something else
    <?php } ?>
    <?php endwhile; ?>

    Thanks

    You sure can, give it a go!

    Sorry on closer inspection no that won’t work but you very close it should be:

    <?php while (have_posts()) : the_post(); ?>
    <?php if (has_tag('keyword')) { ?>
    	<div class="post">
    		<h3><a>"><?php the_title(); ?></a></h3>
    		<small><?php the_time('l, F jS, Y') ?></small>
    		<div class="entry">
    			<?php the_content() ?>
    		</div>
    	</div>
    <?php }elseif (has_tag('keyword-2')) { ?>
    something else
    <?php }elseif (has_tag('keyword-3')) { ?>
    something else
    <?php }else{ ?>
    something else
    <?php } ?>
    <?php endwhile; ?>

    Thread Starter invisiontech

    (@invisiontech)

    Excellent, thank you much for help!!! You’re great.

    Question, from the code you posted above, what if I wanted to show the

    <?php }else{ ?>
    something else
    <?php } ?>
    <?php endwhile; ?

    On post page as well as the non post pages like category and home page as well? Is that possible?

    Thank you again.

    That loop will work on any of your pages: archive page, single post page or front page.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Tag Conditional not working…’ is closed to new replies.