WordPress.org

Ready to get started?Download WordPress

Forums

Check for a specific category for a post (2 posts)

  1. golb0016
    Member
    Posted 1 year ago #

    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'>";}
    }
    ?>

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    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'>";}

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags