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.
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.
1 2 3 4 5 6 7 |
<?php query_posts('showposts=5&post_status=future'); ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h2><?php the_title(); ?></h2> <span class="datetime"><?php the_time('j. F Y'); ?></span></p> <?php endwhile; else: ?><p>No future events scheduled.</p> <?php endif; ?> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content, $matches); $first_img = $matches [1] [0]; if(empty($first_img)){ //Defines a default image $first_img = "/images/default.jpg"; } return $first_img; } |
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:
1 2 3 4 5 6 7 8 9 |
<?php query_posts('showposts=5'); $ids = array(); while (have_posts()) : the_post(); $ids[] = get_the_ID(); the_title(); the_content(); endwhile; ?> |
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:
1 2 3 4 5 6 7 |
<?php query_posts(array('post__not_in' => $ids)); while (have_posts()) : the_post(); the_title(); the_content(); endwhile; ?> |
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.
1 2 3 |
<?php query_posts('meta_key=review_type&meta_value=movie'); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> |
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!
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php function filter_where($where = '') { $where .= " AND post_date >= '2012-09-16' AND post_date <= '2010-02-01'"; return $where; } add_filter('posts_where', 'filter_where'); query_posts($query_string); while (have_posts()) : the_post(); the_content(); endwhile; ?> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php if (have_posts()) : ?> <?php $count = 0; ?> <?php while (have_posts()) : the_post(); ?> <?php $count++; ?> <?php if ($count == 2) : ?> //ADVERT CODE HERE <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_excerpt(); ?> <?php else : ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_excerpt(); ?> <?php endif; ?> <?php endwhile; ?> <?php endif; ?> |
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:
How about creating your own loop to maintain the blog? The following code uses WP_Query and accomplishes just that:
1 2 3 4 5 6 7 8 9 |
<?php $myPosts = new WP_Query(); $myPosts->query('showposts=10'); while ($myPosts->have_posts()) : $myPosts->the_post(); ?> the_title(); the_content(); endwhile; ?> |
#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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php /* Template Name: Archives */ ?> <?php get_header(); ?> <h2><?php $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'"); if (0 < $numposts) $numposts = number_format($numposts); ?> <h2><?php echo $numposts.' recipes published since October 06, 2008'; ?> </h2> <ul id="archive-list"> <?php $myposts = get_posts('numberposts=-1&'); foreach($myposts as $post) : ?> <li><?php the_time('m/d/y') ?>: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <?php get_sidebar(); ?> <?php get_footer(); ?> |
If you want to list blog posts from a specific category, you can apply this hack:
1 2 3 4 5 6 7 |
<?php foreach((get_the_category()) as $category) { $thecat = $category->cat_ID . ' '; query_posts('child_of='.$thecat); if (have_posts()) : while (have_posts()) : the_post(); //Classic WP loop endwhile;endif; ?> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $sticky = get_option('sticky_posts'); rsort( $sticky ); $sticky = array_slice( $sticky, 0, 7); query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); if (have_posts()) : while (have_posts()) : the_post(); the_title(); the_excerpt(); endwhile; endif; ?> |
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:
1 2 |
<?php $i = 1; ?> <?php if(have_posts()) : ?> |
Next, search for
Found it? Paste this line right above it as well:
1 2 |
<?php $i++; ?> <?php endwhile; ?> |
Now, we’re ready to paste Adsense code. It can be used anywhere within the loop:
1 |
<?php if ($i == 1) { ?> [PASTE ADSENSE CODE HERE] <?php } ?> |
Comments are Closed