1

To explain... I have a loop that is getting the posts from a few different categories and for one category, I want to change the link that gets wrapped around the title.

For example:

I have 3 posts from the category "Events", I want the first 2 Events posts to link to their specific page and I want the latest, event #3, to link to a different page on the website (NOT the individual post's page like event #1 & 2).

So in the loop, I have something along the lines of:

<a href="<?php echo get_permalink();?>"><h1>Title</h1></a>

And I want to change switch 'echo get_permalink' to '/events/signup' the latest Events posting at the same time, keeping 'echo get_permalink' for all the other category posts in the loop.

What's the best way to approach this?

2

You can retrieve the index of the post in the loop with $wp_query->current_post. From that, you can check with a simple if-statement:

/*Check if post index is 2 (you referred to the 3rd post, given indexing starts from 0, the 3rd post index would be 2 */
if ( $wp_query->current_post == 2 ) : 
   /* Do something */
else:
   /* Do something else */
endif;
  • I figured out this, however my account doesn't have enough reputation to post an answer... So unfortunately I cannot share here. – sk03 Nov 1 '17 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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