Forums

Using is_tag with if else statement (7 posts)

  1. viktorix
    Member
    Posted 1 year ago #

    I'm trying to figure out a way to display two different ad codes based on the article tag. Here's what I was trying to do but it's not working, would really appreciate some feedback. This is part of sidebar.

    <?php if (is_tag('example')) {
    JS AD CODE HERE
    }
    else { ?>
    ALT JS AD CODE HERE
    <?php } ?>

    Thanks in advance!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    is_tag('example') translates to "if this is the page listing all posts with the example tag"? Is that what you were after?

  3. viktorix
    Member
    Posted 1 year ago #

    Hey esmi,

    Yes, if a post contains "example" tag it will show ad code #1, but if it doesn't contain it then show ad code #2.

    I have to do this due to some js conflicts on a service that we use while they are looking into the problem.

    Thanks for your reply,
    Viktor

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    if a post contains "example" tag it will show ad code #1

    that would be has_tag()

    http://codex.wordpress.org/Function_Reference/has_tag

  5. viktorix
    Member
    Posted 1 year ago #

    Thanks for the info. So if I change is_tag to has_tag, will it work? I just want to make sure if else is setup right.

  6. alchymyth
    The Sweeper
    Posted 1 year ago #

    just make sure to switch cleanly from php to js etc;

    example:

    <?php if (has_tag('example')) { ?>
    JS AD CODE HERE
    <?php }
    else { ?>
    ALT JS AD CODE HERE
    <?php } ?>
  7. viktorix
    Member
    Posted 1 year ago #

    Excellent. Thanks a lot for your help. Really appreciate it.

Topic Closed

This topic has been closed to new replies.

About this Topic