Get the FULL version

WordPress: get posts within the loop

Final Considerations

The same techniques could be applied to archive.php, tag.php and search.php as they work the same way as the index.php but returns different posts.

Some if statements had a is_paged() function call. This was used to determine whether the posts are on the first page, meaning that:

//added if statement
<?php if($postCounter % 2 != 0 && !is_paged()) //get odd posts only on the first page
    //Your code here, probably an advertisement.
} ?>
//WordPress Loop ends here
<?php endwhile; ?>

The above executes a code for all the odd posts that are on the first page. For the second and beyond pages, any code inside the if statement won’t execute. That’s because is_paged() would return true.

These codes can also be used to style the posts differently based on the if statement, although, we need to place it in a different location inside the loop. This way, instead of inserting the code at the end of the loop, replace the line that has the_content() function call with the following if statement.

//add this if statement
<?php if($postCounter % 3 == 0) //for every 3 posts
    ?> <strong> <?php the_content(); ?> </strong> <?php //display bold text
   /*don't forget to add the_content here,
    *so other posts are also displayed*/
//Rest of WordPress Loop...

By using this techniques, it is possible to get the desired posts withing the loop and change some specific parts from them. For example, if you want to change how the title for each 5 posts, just make a if statement that wraps the title code and then add your customized changes for the title inside the if statement block.

OK, that’s it, I hope it was clear enough.

Pages: 1 2 3 4 5

Be the first to leave a comment!

Leave a Comment

Post Comments RSS