Forums

Tag Conditional not working... (7 posts)

  1. invisiontech
    Member
    Posted 7 months ago #

    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.

  2. tomontoast
    Member
    Posted 7 months ago #

    <?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; ?>
  3. invisiontech
    Member
    Posted 7 months ago #

    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

  4. tomontoast
    Member
    Posted 7 months ago #

    You sure can, give it a go!

  5. tomontoast
    Member
    Posted 7 months ago #

    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; ?>
  6. invisiontech
    Member
    Posted 7 months ago #

    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.

  7. tomontoast
    Member
    Posted 7 months ago #

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

Reply

You must log in to post.

About this Topic