2

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.

2
  • 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, 2012 at 22:10
  • Yes, the top 2 images. They are reversed and I can't figure that part out either :/
    – diventato
    Oct 9, 2012 at 22:13

2 Answers 2

0

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.

3
  • Thanks Rarst, but I am getting syntax errors after moving to after the 'while' line.
    – diventato
    Oct 9, 2012 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, 2012 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, 2012 at 22:44
0

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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