The WordPress Loop

The WordPress Loop

This article will cover the basics to using the Wordpress loop, we will also look at custom port types and how to apply them to your templates and websites.

The Basic Wordpress Loop.

The loop for general posts can be written in two ways, we will only focus on one of those ways but to you show the two we will add the code below fro both the techniques.
Code example.
$args = array(
	"post_type" => "post",
	"cat" => "general"
);

$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) :
	$the_query->the_post();
	echo "<li>" . get_the_title() . "</li>";
endwhile;

/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren"t stomping on the 
 * original  and it does not need to be reset.
*/
wp_reset_postdata();

The above technique is the first way of using the loop, the code above has been taken from the default twenty twelve template which comes with the default Wordpress install.

Notice the while loop, if you are not familiar with PHP or any popular programming language, the while loop is one of the available loops a developer can choose from.
The while loop continues until the set condition has changed, in this case the loop will continue until no more posts are available to show.
The next important part is the_post(); this will call all the required information we need to display the post to the user.
Inside the post we have a number of functions, such as the_title(), the_content() and so on.
These functions will allow us to show different parts of the post, the title is displayed using the_title(), but if we want the title but don't want it to print we will use get_the_title().
I will write down all the available functions at the end of this article.
The final part to this loop is the endwhile, this is used to end the while loop, an alternative way of writing the while loop is with brackets such as
Code example.
while(1 == 2){

}

Ok the next loop is maybe the most powerful loop in Wordpress, it will give use full access to all posts and post types.

This type of loop is known as WP_QUERY(), the code is below and it will allow us to easily display any post we want.
Code example.
$args = array(
	"post_type" => "post",
	"cat" => "general"
);

$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) :
	$the_query->the_post();
	echo "<li>" . get_the_title() . "</li>";
endwhile;

/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren"t stomping on the 
 * original  and it does not need to be reset.
*/
wp_reset_postdata();

The Wp_query is a global in the Wordpress design, it can be directly accessed by typing in the following.

Code example.
Global $wp_query;

print_r($wp_query);

The most important part of this technique is the array which is added to the WP_Query function, with this we can choose the post type, the post count, the category and so on. We can choose any attribute we want which is associated to the posts.

Inside the while loop is the same as the first technique, the next thing we will look at is displaying custom posts.

Custom Posts.

You can see that this website has been built using Wordpress, we have added a number of custom post types such as the Collections and the Basics.

To display a custom post type you should do the following:
Code example.
$args = array(
	"post_type" => "customtype",
);

And it's that simple.

We simply specify what post type we want using the post_type attribute and thats it.
Now if you want to select a custom taxonomy, then add 'mytax' => 'categoryname'
Code example.
$args = array(
	"post_type" => "customtype",
        "taxonomy" => "general"
);

So as you can see above, the taxonomy is what we use to choose the custom category specified in the Wordpress admin.

You can modify the above code to suit your requirements.
I hope this was helpful and it helped you with any problems you may have.
If you have any questions, then feel free to add them below.

Useful Functions for the Loop

the_title();

This will allow you to get the title of the post and it will print it for you

get_the_title();

This will return the title as a string which you can then use and print when where ever you want.

the_content();

This will print the post content to the page in the loop

get_the_content();

This will return the post content as a string which you can then use

the_permalink();

This will get the link to the post and print it.

get_permalink();

This will get the link to the post and return it to a string.

the_excerpt();

This will print a summary of your post, the content has been extracted for the content of your post

get_the_excerpt();

This will return the excerpt to a string.

the_category();

This will print all the categories associated to the post

get_categories();

This will return an array of the categories associated to the post.
For a full list of available Wordpress functions, go here http://codex.wordpress.org/Function_Reference#Category.2C_Tag_and_Taxonomy_Functions

 

comments powered by Disqus