Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
In WordPress: Creating Custom Widgets and Plugins with PHP, Drew Falkman teaches PHP developers how to create custom functionality for WordPress 2.0 through 3.0 using widgets and plugins. This course starts by installing and setting up WordPress 3.0 on both Mac and Windows, then provides an in-depth look at tasks related to these WordPress add-ons: installing and administering, building and customizing, creating editable options and database tables, working with posts and pages, and utilizing jQuery and AJAX. There are also tutorials dedicated to promoting a widget or plugin, adding security, and localizing the interface. Exercise files are included with the course.
As we've learned, every page in WordPress has its own loop. This contains the data relevant to that page. At some point, we might want to query our own loop and essentially access the same kind of post and page data, but not have it be whatever is on the current page. There's a special class built into WordPress called WP_Query that we can use for this. If you remember, we've built this top_ posts widget, which will output information about each post that's on the current page. So as you can see, on the default page, it's outputting the two items. But if I go to the About page, it outputs the About page itself, which is stored in that loop.
What it's doing is it's calling these methods, have_posts and the_post, on the special default WP_Query class that's built into WordPress. This class gets its data in the header, and it gets it depending on the file you're in and any URL variables that might have been passed to it. If we want to create our own object, we can essentially create a variable and set it equal to the WP_Query class.
This class is made to store this data, and it's much cleaner and easier and better to use than accessing the database directly. So what we do to populate it is we simply call the get_posts method of that object. This will go out and get posts. You can apply filters if you want to, and we'll talk about that later. For now, it's just going to populate it with the default post data, essentially what would display on the first page: any sticky posts and then other posts in descending order.
To output in our loop, we now need to access the post data from this query. So just change it so you are calling the same methods of the query. So now it's getting data from our posts. The post itself will always populate the default post, and that is what's accessible by these template tags. So you don't have to do any changes to the template tags; just make sure you prefix your have_posts and the_post with your specific post object.
So let's go back to our site, and let's go ahead and refresh the homepage. You'll see I have "Update for Today" and "Hello world!" These are the default posts, which is what I'd expect because those are the post available on the main page. However, if I go into the About page, you'll see that I still have the same posts, because it's now getting these posts from our get_posts call. So, WordPress offers us the ability to create our own customized post and page data by using this WP_Query class.
We can then call the get_posts method to populate it, and we loop through it just like we did with the main loop, using have_posts and ultimately setting the post information to the current page by calling the_post.
Find answers to the most frequently asked questions about WordPress: Creating Custom Widgets and Plugins with PHP.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.