D5 Creation Blog

WordPress Loop Hack

12 WordPress Loop Hacks You Can Do Without Plugins

The loop is a critical element of the WordPress system without which a site will not work properly. It is essentially a PHP code that the system uses to display content, so it’s the main process.
Tweaking this set of powerful functions may result in some even more advanced possibilities for your blog. Let’s explore twelve ways to hack into the loop right now.

#1. List Upcoming Posts

Our beloved WordPress enables us to create a blog post and set a specific time for publishing later. If you want to ensure that the readers return to your page and subscribe to it, providing a list with the future posts is a great move.

The following code should be inserted in any place of the current WordPress theme to achieve displaying them. It commands the system to display 5 upcoming posts ('showposts=5) but you can increase this parameter up to 10.

#2. Show a Loop of Images

This tweak is great for those who want to make their blog more unique. Most blogs show post excerpts with an image on the starting page but the code below helps to create an entire gallery of posts with their lead images.
The gallery is created by creating a function that grabs the lead image and returns its location. Open functions.php and insert this function. It’s called catch that image and it does exactly what we need.

#3. Use Multiple Loops on One Page Without Duplicating Posts

Many current WordPress themes utilize multiple loops on the home page, which can result in duplicated posts. Clearly, this is something to be avoided. Open your index.php file and get ready: you are about to paste two functions there that will eliminate this problem once and for all.

The first function targets the first loop used on the home page. The trick is simple: using parameter called showposts, we determine the number of the recent posts (5 in the example) using query_posts function:

Now, proceed to applying the next function that targets the second loop. It works with the remaining posts, so 5 posts we identified above remain untouched. The query_posts functions is also used here:

#4: Output Posts with a Particular Custom Field and Particular Value

Many WordPress users want to do that because of insane popularity of custom fields. If you’re one of them, use the code below. It utilizes query_posts function along with a number of other parameters that define particular values.

#5. Retrieve Posts Published Between Particular Dates

This simple tweak can be really useful in search of posts. If you want to get some information about the posts you published, say, last year, retrieving them might be a challenge. The code below simplifies the search. Just don’t forget to change dates in the example!

#6. Advertise Your Blog by Adding Ads after Posts

As a blogger, you know the importance of promotion very well. Inserting ads after posts is one good way to go that can get a decent amount of clicks for you. Loop is here to help with that with the following code:

#7. Show Old but Popular 1-Year-Old Posts

If your blog uses evergreen content, there is a good chance that some of your old posts can still be popular. For example, a blog of an essay writing service can feature how-to articles that help to write better academic works that contain relevant content regardless of university and time of year. However, they can disappear in the sea of the recent posts because the system displays new items first.

To get your blog to display 1-year-old posts and even older, open single.php file and paste this hack:

#8. Create Own Loop with WP_Query

How about creating your own loop to maintain the blog? The following code uses WP_Query and accomplishes just that:

#9. Create a Template with a List of All Posts
As we described above, visitors can have trouble finding posts that you created in the past. An archive page that contains all entries published on your blog from the very beginning to this day might be a good solution for this problem. Here’s how to create it:

#10. Retrieve Posts in Particular Category

If you want to list blog posts from a specific category, you can apply this hack:

#11. Get Only Sticky Posts

They are a great feature that quickly became popular among the users of WordPress. To get 7 latest sticky posts in the loop, use the following code:

#12. Show Google Adsense on the First Post

Google is retiring its Adsense Plugin, so we need to find ways to insert it using a code. Open index.php file in the theme and look for this line: . When you locate the line, paste this hack right above it:

Next, search for
Found it? Paste this line right above it as well:

Now, we’re ready to paste Adsense code. It can be used anywhere within the loop:

Author : Tom Jager

Tom Jager is professional blogger. He works at Awriter.  He has degree in Law and English literature. Tom has written numerous articles/online journals

Comments are Closed