eLert Gadget
GOT SOMETHING IMPORTANT TO SAY? ...
Your Hotline to Readers' Desktops
4 WAYS TO LOOP WITH WORDPRESS Published: 12/05/11
Vote Positive Positive ( 0 ) Vote Negative Negative ( 0 ) Question
At the heart of the WordPress theme template is the venerable WordPress loop. When you’re looking at your index.php file, for example, the loop is the part that typically begins with if(have_posts()) and contains all the tags and markup used to generate the page. The default loop works perfectly well for most single-loop themes, but for more advanced designs with stuff like multiple and custom loops, more looping power is needed. Fortunately, WordPress provides plenty of flexibility with four ways to loop: Default Loop Loop with query_posts() Loop with WP_Query() Loop with get_posts() Each of these looping methods is useful in a variety of situations. They share a lot of the same underlying functionality, and the query parameters are essentially the same. Collectively, these four techniques enable simple loops, multiple loops, and custom loops in your WordPress theme template. A good place to find a default loop, for example, is in your theme’s index.php file. Its purpose is to loop through the posts stored in the database and echo their contents to the browser. Using WordPress’ template tags, it’s easy to display post titles, content, meta info, and much more. That said, let’s examine the four ways to loop with WordPress. The Default Loop The default WordPress loop looks something like this:
id="post-">

id="post-">

Not Found

So what makes it “default”? Mostly because it uses the default query to loop through post content, making it the loop used like 99% of the time for most themes. It tells WordPress to loop through posts and display the information according to context, and as called by the various template tags (the_title, the_content, et al). There are tags available for just about any type of data stored in the database. Based on the query that is sent, the default loop will display a certain number of posts from a certain category from a certain date, and so on. For example, the number of posts displayed in the first part of the loop is specified in the WP Admin. So if someone requests the second page of your “Awesome” category, that information is sent via the query, along with the number of posts, theme template file, and so on. So the default loop is perfect if you’re happy with the query that is sent, but it is also possible to customize the query and generate an entirely different set of posts. Loop with query_posts() The query_posts function enables us to modify the query and display our desired results. We can either override the entire query or keep it around and just change a few parameters. Here’s an example where query_posts is called before the default loop to exclude a specific category: Say you have a default loop in your index.php theme template, but you want to change the number of posts, exclude two categories, and display the results in ascending order. Easy. Just add some query_posts action before the default loop and wp_reset_query immediately after, like this: Here we are keeping the original query around and just overriding a few parameters. There are many parameters available, so customizing any default loop is accomplished quite easily. If we wanted to completely override the original query, we would replace the second line with something like this: $posts = query_posts('posts_per_page=3&cat=-6,-9&order=ASC'); Notice here that we’ve removed the $query_string from the query_posts parameters. This essentially erases the default query and replaces it with one that contains only those variables included in query_posts. This means no paging information will be available, so remove the original query only if you know what you’re doing. When to use? Use query_posts to modify the type of posts that are returned for a single loop. It’s perfect for limiting the number of posts, excluding posts from a certain category or tag, and so on. If more than one loop is required, multiple query_posts loops could work, but there is a much better way to do it using WP_Query. Loop with WP_Query() For complete control over the customization of any number of loops, WP_Query is the way to go. When used to modify a default loop, it looks similar to query_posts. For example, let’s exclude a specific category using WP_Query: have_posts()) : $custom_query->the_post(); ?>
id="post-">

It also accepts the same parameters as query_posts, so modifying stuff like number of posts, included/excluded categories, and post order looks quite familiar. As seen in the following examples, WP_Query makes it easy to customize the loop by simply changing the parameter: $custom_query = new WP_Query('cat=-7,-8,-9'); // exclude any categories $custom_query = new WP_Query('posts_per_page=3'); // limit number of posts $custom_query = new WP_Query('order=ASC'); // reverse the post order As expected, we can combine parameters with WP_Query using the same syntax as both query_posts and get_posts. Here’s the equivalent of our query_posts example: $custom_query = new WP_Query('posts_per_page=3&cat=-6,-9&order=ASC'); Notice, however, that with WP_Query, we don’t need the $query_string variable. In addition to using WP_Query to customize the default loop, we can also use it to create and customize multiple loops. Here is a basic example: have_posts()) : $first_query->the_post(); ... endwhile; wp_reset_postdata(); // Loop 2 $second_query = new WP_Query('cat=-2'); // exclude category while($second_query->have_posts()) : $second_query->the_post(); ... endwhile; wp_reset_postdata(); // Loop 3 $third_query = new WP_Query('cat=-3'); // exclude category while($third_query->have_posts()) : $third_query->the_post(); ... endwhile; wp_reset_postdata(); ?> Each of these additional loops may be placed anywhere in your theme template – no need to line them up sequentially. For example, one loop may be placed in your sidebar, another in your footer, and so on. And with the output of each loop easily modified using any of the available parameters, any loop configuration is possible. When to use? Use WP_Query for creating multiple, customized loops. By setting up additional instances of WP_Query in your theme, you can create any number of multiple loops, and customize the output of each. Even so, we don’t always need to break out the big guns, sometimes we just need a few additional loops displayed around the page. So let’s put down the bazookas and gather in the garden for some get_posts tea ;) Loop with get_posts() The easiest, safest way to create multiple loops in your theme is to use get_posts(). Anywhere you need to display a quick, static set of posts, get_posts is the perfect choice. Think 10 recent posts in the sidebar, or 10 random posts in the footer. get_posts makes it easy. Here again is a query to exclude a specific category: -9); // exclude category 9 $custom_posts = get_posts($args); foreach($custom_posts as $post) : setup_postdata($post); ... endforeach; ?> This code creates a loop of all posts except those in the excluded category. Of course, excluding a category is just one way to customize your additional, static loops. By using any of the same parameters accepted by WP_Query and query_posts, it’s possible to create loops that display just about anything you want. Notice, however, that get_posts requires the use of an array for the parameters. The format for multiple parameters looks like this (using our previous example): $args = array('numberposts'=>3, 'category'=>-6,-9, 'order'=>'ASC'); Also notice that we’re using numberposts instead of posts_per_page to limit the number of posts. According to the WP Codex, posts_per_page should work with get_posts, but if it doesn’t just go with numberposts. There is also a showposts parameter that also seems to work fine with get_posts. When to use? Use the get_posts() function to easily create additional, static loops anywhere in your theme. get_posts accepts the same parameters as query_posts, and is perfect for adding static, custom loops to your sidebar, footer, or anywhere else. 30-Second Summary The bottom line for customizing default loops and creating multiple loops: To modify the default loop, use query_posts() To modify loops and/or create multiple loops, use WP_Query() To create static, additional loops, use get_posts() If you’re working with WordPress loops and want to learn more about using them to customize your theme, we cover the topic extensively in our book, Digging into WordPress, which is current with WordPress 3.1 :) Possibly Related Posts6 Ways to Display WordPress Post Content in Multiple ColumnsTwo Ways to Limit the Number of Posts without a PluginEasy Custom Feeds in WordPressAjax Requested Page Return Only ContentThumbnail Based ArchivesSpecify Unique CSS File Per PostDeclare Multiple Widgetized AreasCustom Comments HTML Output via feedproxy.google.com Working with the Wordpress "Loop" Posted via email from rightinfo's posterous

Read More

 

HOW EASY IS IT FOR YOUR CUSTOMERS TO CONTACT YOU... Published: 01/11/12
How Easy is it for Your Customers to Contact You... Recently I came across another wordpress plugin that makes it very easy for a visitor to leave you a spoken comment, or they can ask you a question. All they need is a computer with a microphone, they then click on the link on the right hand side of your site to record a message in the browser up to 10 minutes in length. ...

Comments (0)    Read More
  • Vote Positive Positive (0)
  • Vote Negative Negative (0)
  • Question
Gadget Name: Better Business
AFFILIATE AREA Published: 26/10/12
Affiliate Area Join or Sign-in to the Best Biz Info 4UAffiliate ProgramEach Sale of the Fix It Service will give you $100.00NZ and I will teach you how to find easy business clients and to know before you contact them, if their site is vulnerable! Other products coming. ...

Comments (0)    Read More
  • Vote Positive Positive (0)
  • Vote Negative Negative (0)
  • Question
Gadget Name: Better Business
WHAT IS SEO AND WHAT IS COINCIDENCE? Published: 19/06/11
I haven’t posted much lately. Mostly that is down to a new role I have as a Web Developer now. As per usual I am not going to give away the name, but it is a fairly large media company here in NZ. Obviously it is working within their digital department, “digitizing” what they currently have in print. I have only worked there just over a week, and already I see things th...

Comments (0)    Read More
  • Vote Positive Positive (0)
  • Vote Negative Negative (0)
  • Question
Gadget Name: Better Business
AMAZING BED AND BREAKFAST - CHANGING LANES PUKEKOHE NZ Published: 09/06/11
If it's time to take a break, a weekend away, a romantic couple of nights for husband and wife, or maybe your honeymoon then check out http://changinglanespukekohenz.cz.cc A beautiful, safe, romantic honeymoon hideaway. ...

Comments (0)    Read More
  • Vote Positive Positive (0)
  • Vote Negative Negative (0)
  • Question
Gadget Name: Better Business
PLUGIN TO REMOVE COMMENTS COMPLETELY FROM WORDPRESS Published: 08/06/11
via wpengineer.com Sometimes Wordpress is perfect for what you want except you do not need Comments! Yes you can manually disable them, and then you are backwards and forwards editing because you forgot to turn them off. Now you can have a plugin and have them removed site wide. Posted via email from rightinfo's posterous ...

Comments (0)    Read More
  • Vote Positive Positive (0)
  • Vote Negative Negative (0)
  • Question
Gadget Name: Better Business
PLUGIN TO REMOVE COMMENTS COMPLETELY FROM WORDPRESS Published: 08/06/11
via wpengineer.com Sometimes Wordpress is perfect for what you want except you do not need Comments! Yes you can manually disable them, and then you are backwards and forwards editing because you forgot to turn them off. Now you can have a plugin and have them removed site wide> Posted via email from rightinfo's posterous ...

Comments (0)    Read More
  • Vote Positive Positive (0)
  • Vote Negative Negative (0)
  • Question
Gadget Name: Better Business
PLUGIN TO REMOVE COMMENTS COMPLETELY FROM WORDPRESS Published: 08/06/11
via wpengineer.com Sometimes Wordpress is perfect for what you want except you do not need Comments! Yes you can manually disable them, and then you are backwards and forwards editing because you forgot to turn them off. Now you can have a plugin and have them removed site wide> Posted via email from rightinfo's posterous ...

Comments (0)    Read More
  • Vote Positive Positive (0)
  • Vote Negative Negative (0)
  • Question
Gadget Name: Better Business
4 WAYS TO LOOP WITH WORDPRESS Published: 12/05/11
At the heart of the WordPress theme template is the venerable WordPress loop. When you’re looking at your index.php file, for example, the loop is the part that typically begins with if(have_posts()) and contains all the tags and markup used to generate the page. The default loop works perfectly well for most single-loop themes, but for more advanced designs with stuff like multiple ...

Comments (0)    Read More
  • Vote Positive Positive (0)
  • Vote Negative Negative (0)
  • Question
Gadget Name: Better Business
CREATE YOUR OWN FUNCTIONALITY PLUGIN Published: 12/05/11
via feedproxy.google.com Great tutorial on writing plugins for your wordpress site instead of overloading the functions.php file. Posted via email from rightinfo's posterous ...

Comments (0)    Read More
  • Vote Positive Positive (0)
  • Vote Negative Negative (0)
  • Question
Gadget Name: Better Business
PDFILL PDF EDITOR WITH FREE PDF WRITER AND FREE PDF TOOLS - WWW.PDFILL.COM Published: 12/04/11
via pdfill.com Need tools to work with PDF files? Posted via email from rightinfo's posterous ...

Comments (0)    Read More
  • Vote Positive Positive (0)
  • Vote Negative Negative (0)
  • Question
Gadget Name: Better Business
To leave your comments/rating please login
  • eLert Gadget Account Holders:
  • Not A Member Register Here:
Your E-mail ID: Your E-mail ID:
Your Name:
Please enter the letters that you see on the above image.

 

There are no comments for this elert. Be the first to comment.