WordPress.org

Ready to get started?Download WordPress

Forums

The Loop (5 posts)

  1. nerdland
    Member
    Posted 1 year ago #

    The loop is still a little high-concept for me, so forgive me if I'm stupid here. But in my header.php, I'm trying to display a featured image in the header, if it's set. So I'm using something along the lines of

    <?php if ( has_post_thumbnail() ) {
        the_post_thumbnail(); }
    else { ?>
        <p>No thumbnail</p>
    <php } ?>

    This works great on all of my pages. But on the archives, things get a little fuzzy. In my mind, that code should return "No thumbnail" on the archive pages. But it doesn't. It returns the thumbnail of the last-grabbed post on the page. I know that this has something to do with the loop, I just don't know the best way to fix it.

    I do realize that functionally, I could fix my problem by just wrapping that with if (!is_archive()) and then setting a rule for archive pages separately, but is that the best solution? I'm trying to wrap my head around the loop and understand why this happens in the first place, and figure out if there's a better solution to stop it.

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    I'd say just wrap the whole thing in a is_single()

  3. TCR
    Member
    Posted 1 year ago #

    Seconded. this rules out other pages which might have more than 1 post on the page. category listings etc.

  4. nerdland
    Member
    Posted 1 year ago #

    Yeah, I do know that's an option. It just seems like it isn't quite the right way to do it. But maybe it is, and I'm just thinking about it in a weird way. I'm trying to understand the loop a little better and figure out why, exactly, it behaves that way.

    It doesn't seem logical, in my mind, that a function asking if a page has a post thumbnail should return true on an archive page, when the archive page itself obviously doesn't.

  5. nerdland
    Member
    Posted 1 year ago #

    Thanks, though. If I do have to go with that solution, I'll at least feel a little better about it. Haha.

Topic Closed

This topic has been closed to new replies.

About this Topic