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.
At some point when you are working with WordPress, you're going to come to a point where you want to work with the posts. This is, after all, blog software, and post of blogs are all about. WordPress has a special construct to hold this data. It's called the loop. It's used to hold page data as well. In the video, we are going to discuss this key piece of the WordPress architecture. I've created a simple widget here that's going to be used on the front-end. Note, I've use the old widget methodology. This way has been deprecated, and we looked at the new widget API, but it's kind of good to see how it works, in case you see this in some other plugin.
What I am going to do is I am going to loop through the loop, and I am going to output information. So, by default, the loop is going to hold whatever data is being used on that page. On the front page, it's going to have however many post the user specified. The default is 10. It's going to start with sticky posts at the top, ones that have been marked as sticky, and then it's going to go through in descending order of when they were entered. On other pages, it's going to differ. If you go to a custom page, for example, the loop will just be populated with the data from that page.
If you go to a specific post page, it will be populated with just that post. So let's go ahead and output this widget. This widget is essentially going to show us what's in the loop for each particular page. The way you loop through it is you actually don't access anything called the loop or anything. There's a special function called have_ posts that we can use to loop through it. So the first time you use it; it's always going to go out and get the next available post. If there are no posts at all in the loop, then it will return nothing.
So what we can do is we can use this in an if statement, and say if have posts and then do some particular form of code execution. So if there are no posts, after this it will stop, and we'll be done. However, if there are any posts, we can use the while loop and basically say as long as there's another post in here, go ahead and loop through it. What we are going to do then is we are going to access the posts by calling the special function, the_post.
That will queue up the post and populate a number of template tags with post data. If you remember template tags, these are these functions that we can use to access data. They're built into WordPress. A lot of these work with data that's in the current value of the loop. For example, this comment data is going to come from comments that are in the loop, the_content is going to come from existing content for the current item in the loop, the_title--same thing.
So we can use these template tags to access data inside of the loop. So in our function here, we have our if statement and our while statement looping through the_post. So now we can output what's inside of the post. So we are going to create a div tag for each entry, and inside of this div, we are going to have a link--a href--and the link is going to refer to the permalink for the post, and the way we access that is using a template tag--echo the_permalink. There is our link.
We'll set the title property of our href to the title of the post, and again, we use a template tag. We'll set the same value in between the open and closing a. So, php echo the_title. The last thing we are going to do is we are going to output however many comments are in this current post. So we'll create our parentheses, and then we'll put it in here using another function call comments_number.
The last thing we need to do is close the loops that we have going on. So after this final div tag, we'll say endwhile, which will end our loop, and endif, which will end our if statement. So this is our basic loop that will go through the loop for whatever is on the current page. So, let's go ahead and go into the WordPress admin. We are going to look for our plugin called top posts. We are going to activate it.
This will register the widget. So now we need to go into the Widgets page, look for our Top Posts widget, here it is, and drag it into our web site. If we go down to the front of our web site, we should see our plugin in action. Indeed, here it is. You can see Posts on this page, our Hello world, and there are 3 comments. Note that comments number doesn't just output the number, but it actually outputs the word 'comments' as well. So it's meant for visual display.
So, let's go back in, and let's add another post. If we go to the dashboard, we have this QuickPress plugin. We can use this to quickly add a post. We'll just call this "Update for Today," "More sunny weather in Ventura," Publish. So, now it's been published. If we reload, you can see it's been updated. It's been moved to the top of the list, because by default it's in descending order, and you can see down in our output, it does the same.
If I were to go into one of these, I would also see only that item. Notice the other one disappeared, because on this particular page, the loop was only populated with that data. It's true for pages. The page information is stored just like a post is, inside of the loop, so you can see About gets listed for the loop on this page. If you ever want to start over in your loop, if for some reason you need to do multiple loops, you can use a function called rewind posts to go back to the beginning.
So the WordPress loop offers information for WordPress developers to access the posts and the pages inside of WordPress. We can use this have_posts method to iterate through them, the_post to get it, and then any number of template tags to access all the data inside of it.
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.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.