Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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="<?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="<?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.

share|improve this question
    
Do you mean the topmost images to the right of the logo? They link to different pages right now (although links seem to be reversed for what images say). –  Rarst Oct 9 '12 at 22:10
    
Yes, the top 2 images. They are reversed and I can't figure that part out either :/ –  diventato Oct 9 '12 at 22:13
add comment

2 Answers 2

up vote 0 down vote accepted

For both loops move

<a href="<?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.

share|improve this answer
    
Thanks Rarst, but I am getting syntax errors after moving to after the 'while' line. –  diventato Oct 9 '12 at 22:38
    
@diventato well, you need to adjust things accordingly, probably by closing and opening PHP tags ?><a href="<?php the_permalink(); ?>"><?php. –  Rarst Oct 9 '12 at 22:42
    
thanks for pointing me in the right direction as I am doing just that :) I know now that I cannot call a permalink like that until after I tell it what to query. –  diventato Oct 9 '12 at 22:44
add comment

After some pointers from my homie Rarst, I was able to make this work. I changed

this

<a href="<?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>

to this

<?php $the_query = new WP_Query( 'cat=100' ); ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php the_permalink(); ?>">
    <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
        the_post_thumbnail();
    }
endwhile; ?>
</a>  
<?php wp_reset_postdata(); ?>

As Rarst mentioned, the permalink wasn't doing as it should due to the fact that it was not placed inside the loop and after the post query. After the query was called, the permalink had a place to link to... the post thumbnail.

I'm still experimenting with php even though I've been creating Wordpress-driven sites for years now. There is always something to learn in the wonderful world of web design :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.