Forums

[resolved] Display an image on post page if NOT specific category (4 posts)

  1. caddyx
    Member
    Posted 2 years ago #

    I'm just a WordPress user, but am trying to may some tweaks to my pages. On my post pages (single.php) I would like to display an image in the post unless it is in a particular category. Just picking up tidbits here and there, this is what I tried:
    ------------

    <?php
    $findme   = 'My Category';
    $pos = strpos($category->cat_name, $findme);
    
    if ($pos === false) {
        echo
    '<a href="http://www.test.com/"><img class="alignright right size-full wp-image-34" title="Test" src="http://www.test.com/test.gif" alt="Image Link" width="228" height="152" /></a>';
    }
    ?>

    -------------
    The result I am getting is that it displays the image no matter what the category or categories of the post. When I did "echo $pos", it didn't display anything.

    Any help would be greatly appreciated. Specific code would be wonderful! I don't really know PHP and am just getting my feet wet.

    Thanks!

  2. vtxyzzy
    Member
    Posted 2 years ago #

    See if this will do what you want:

    <?php
    $findme   = 'My Category';
    if (!in_category($findme)) {
        echo
    '<a href="http://www.test.com/"><img class="alignright right size-full wp-image-34" title="Test" src="http://www.test.com/test.gif" alt="Image Link" width="228" height="152" /></a>';
    }
    ?>
  3. caddyx
    Member
    Posted 2 years ago #

    Yes! It works perfectly. Thank You, Thank You, Thank You.

  4. vtxyzzy
    Member
    Posted 2 years ago #

    You are welcome!

Topic Closed

This topic has been closed to new replies.

About this Topic