Multiple instances of Featured Image Query

I have some custom code built into the header of my theme that gets the featured image from a post, displays the image and links to the posts permalink when clicked on by a user.

The problem I am having is that I have this happening on 2 different divs pulling in info from 2 different categories, but when you hover over the link as a user, it uses the permalink from only one of the two images. I am using the “page links to” plugin to redirect the post to a page in the site, so I wonder if this is causing an issue. Below is my snippet getting the featured image in 2 separate divs:

<div id="homeprint">
<a href="https://wordpress.stackexchange.com/questions/67673/<?php the_permalink(); ?>">
<?php $the_query = new WP_Query( 'cat=100' );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
        the_post_thumbnail();
    }
endwhile; ?>
</a>           
</div>

<div id="lookup">
<a href="https://wordpress.stackexchange.com/questions/67673/<?php the_permalink(); ?>">
<?php $the_query = new WP_Query( 'cat=101' );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
        the_post_thumbnail();
    }
endwhile;

// Reset Post Data
wp_reset_postdata(); ?>
</a>         
        </div>

Do I need to have two references of wp_reset_postdata(); or am I just doing this wrong?

The example is here in the header. You’ll see two rectangular images that should link to two different pages. Right now they both link to the same location. Thank you all in advance.

2 Answers
2

For both loops move

<a href="https://wordpress.stackexchange.com/questions/67673/<?php the_permalink(); ?>">

To after

while ( $the_query->have_posts() ) : $the_query->the_post();

the_permalink() is template tag and as such generates what it does, based on global $post variable. In your custom loop it doesn’t contain the post you want until after $the_query->the_post() call.

Leave a Comment