0

I need to have pages where the first post on the page has a special format. I've figured out how to modify loop-home.php to get the result I want outputted on the homepage.

I want to have the same formatting on tag and author pages. I could drop the same code in, but I figure there ought to be a way to add a more generalized function. Suggestions on the best way to do this?

0

If you have done the first part correctly in loop-home.php, you can just simply move that into the appropriate template used by your archive pages.

Taken into account that you have not used custom queries to build your queries on the homepage or any of the archive pages, you can do the following which will work on any of the aforementioned pages

  • Use the build in loop counter $current_post to count your posts. This counter starts at 0

  • Target the first post with the counter and apply your styling to the first post only

Inside your loop, like in loop-home.php do the following

if ( $wp_query->current_post === 0 ) {
    // Do something to the first post
} else {
    // Do something for all the other posts
}

If you only need to target the first post on the first page and not on the subsequent pages, just add !is_paged() to your conditional statement. is_paged() will check whether the current page is a paged page or not

if ( $wp_query->current_post === 0 && !is_paged() ) {
    // Do something to the first post
} else {
    // Do something for all the other posts
}
| improve this answer | |

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.