Today we changed things a bit and added in a lesson on installing and using Sublime and the Emmet and WordPress packages for Sublimt Text 2. There is a separate post on Sublime here:
We then went through the first 3 STEPS of Assignment 3. These are essentially the steps that get the blog and the theme ready to add the code to put loops on the page.
As part of our working through thos steps we also looked at the query_posts() function and downloaded my example page template that uses query_posts:page-test-query-posts.php.zip. When you are using this yourself there are a couple of things to keep in mind:
- You need to download and unzip the file before adding it to your Theme.
- The page template was created with the idea that it would be added to the Twenty Ten theme.
- To see the page that uses query_posts in action you have to assign the “Test query posts” template to a page and then view that page.
- You need to go in and change the code in page-test-query-posts.php so that the part that reads
- $args = array(‘cat’ => 13 ); is changed to
- $args = array(‘cat’ => x ); where x is the id of a category on your blog
You can find the id of a category on your blog by going to the Posts>Categories page in the Dashboard and then clicking on a category and looking for tag_ID=13 in the URL. The number is the category id. (also most browsers show the URL of a link somewhere near the bottom of the browser window if you hover over the link, so you can see the number there).
Before we did the query posts thing we had already added the sample posts as outlined in STEP 2 of Assignment 3.
My files from today can be found in the Class Files page.
Today we are going to focus on the loop again. We will start with going over the setup stuff for assignment #3. Then we will look at two different ways to make a custom loop. But first we will talk about the default query
The WordPress Loop is based on a query made to the database that stores all of the information in WordPress. If no query has been made then there is no information to loop through. Changing the query changes what kind of information is shown in the loop.
By default WordPress makes a query based on what you’re showing. If you’re showing the front page and have set it up to show posts (NOT static) then WordPress will do a query for the most recent posts and the Loop will use that. If you’re on a page WP will make a default query to get the information for that particular page only and the Loop will use that info.
Sometimes however you want different or more information that might be contained in the default query. That is what the next two queries are for.
This function replaces the default query when it’s run. It goes out and gets information based on arguments that you provide and then you can use the loop like normal to display the information.
Download this file (and unzip it), and add it to the theme you’re using. We will write some code that will show the page content and then show all of the posts in a particular category after that.
You can use a similar idea to show posts or excerpts of posts based on other arguments (date, author etc).
One of the problems with query_posts() is that it resets the default query. That makes it difficult to have more than one loop on a page and can mess with existing loops.
WP_Query() to the rescue. It is very similar to query_posts() except that you store the information in a variable. Then you need to use this variable when you’re making the loop.
Here is an example of how you create the WP_Query
$args = array(‘cat’ => 13 );
$ featured_query = new WP_Query( $ args );
But then you use it in a different way.
So for example This is a standard Loop:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
And this is one with the $featured_query from above
<?php if ( $ featured_query-have_posts() ) : while ( $ featured_query-have_posts() ) : $ featured_query-the_post(); ?>
So you add the $featured_query in front of everything.
Then after that you use the loop like normal. But you can use this multiple times throughout the page.