Support » Themes and Templates » Check for a specific category for a post

  • I would like to check the categories for a single post and then display an image for within each post matching a specific category. The following script is not working in the single.php file. I appreciate any help!

    <?php
    $category = get_the_category();
    foreach ($category as $value)
    {
    if ($value == ‘routine maintenance’){echo “<img src=’image.jpg’>”;}
    }
    ?>

Viewing 1 replies (of 1 total)
  • Michael

    (@alchymyth)

    Forum Moderator

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

    if you are checking the category name, try:

    <?php
    $category = get_the_category();
    foreach ($category as $value)
    {
    if ($value->name == 'routine maintenance'){echo "<img src='image.jpg'>";}
    }
    ?>

    if you are checking the category slug, change the one line to:

    if ($value->slug == 'routine maintenance'){echo "<img src='image.jpg'>";}
Viewing 1 replies (of 1 total)
  • The topic ‘Check for a specific category for a post’ is closed to new replies.