- Overview
- Transcript
2.3 Outputting the Section Menu
In this lesson, you'll learn how to output the list of pages in a section using get_pages()
.
Related Links
1.Introduction1 lesson, 00:57
1.1Introduction00:57
2.Creating a Section Menu4 lessons, 27:37
2.1Setting Up the Plugin06:05
2.2Identifying the Parent Page for a Query06:56
2.3Outputting the Section Menu07:06
2.4Hooking the Function to an Action Hook and Adding Styling07:30
3.Customizing the Admin Screens3 lessons, 22:05
3.1Functions for Customizing the WordPress Admin04:43
3.2Editing the Dashboard Widgets10:19
3.3Adding a Meta Box07:03
4.Creating a Custom Posts Display3 lessons, 24:47
4.1Configuring the Query Parameters07:57
4.2Writing the Query11:18
4.3Styling the Query Results With Images05:32
5.Conclusion1 lesson, 02:49
5.1Conclusion02:49
2.3 Outputting the Section Menu
Hello, and welcome back to this Tuts+ Course on WordPress Plugin Development. In this part of the course, we'll continue with our plugin to output a section menu. So, we now have a function that will return the ID of the post at the top of the code hierarchy. And next we need to use that to output some content. So let's first add some commented out text. So, now I'll start writing my function. So, the first thing I want to do is check what page I'm on. Because I don't want to run this on the main blog page. So, firstly I'm checking that we're on the page. And secondly, the way not on plots called the homepage in WordPress conditional text, ie, the block page. Doesn't matter whether that's your website's homepage or not, is home is a check for the block page. So if that isn't the case, the function will continue, and the first thing we need to do is fetch what was output by this function up here. And we'll save that to a variable called, ancestor. So having done that, we set the arguments for the children of the ancestor. So we defined an array of variables called arcs. So, the first one is that we want it to be a child of the ancestor page. We want a depth on a moment let me speak, it calls and let's type these out so we can say depth equals minus 1, so this goes down to one level. You could change that depending on the depth you wanted. And you could output a whole list of different levels. But in my case, I'm just doing one level. And let's tidy that up. And those are my arguments. So, those are my arguments for the get pages function which I'll be running next. And I'm going to save what that output as to another array of variables. So I'm using the get pages function with those arguments and saving it to an array called list pages. Now I want to check if this pages has any values. In other words, has it returned anything, and only if that is the case will I continue and start out putting some content. Because if there are no pages I'll put in my section menu, I don't want anything at top. So now I'm gonna set up a section. And I need to close PHP there. And let's close my section tag so I know where I am as I go along. So I got a section with the class of section menu, and then within that I've got an h2. And again, I'll close that h2 tag, and then within that h2, I want the very first item in my list. So I want the top level page and an h2. So, the post that I'm using for that is this ancestor variable here. So, I'm fetching the permalink of the ancestor post. And then using the title of the ancestor post. And then closing my link. So here you can see I've got a link to the permalink from the ancestor, and then I've got the title of the ancestor using get the title.So that's my h2. And now I need to output a list. So for this, I simply use WP_List pages. Making sure, I open up a page P tags and I need to add X. So I'm using the same marks here for list pages as I used for get pages. Because in get pages they are all the child of that ancestor. So this will just list out the pages that the child of the ancestor, not the ancestor itself, but this code up here, will echo out the title of the ancestor with a link to it. And WP list pages will echo out the title of a post with a link to it. So let me just check, I've got all my P tags in order. And, that's my code. So, I'm checking first, if we're on a page and we're not on the main blog page. I'm pulling in the value that was returned in this first function, and saving it to the ancestor variable. Then I'm defining the arguments for get pages and later on WP list pages. And I'm creating another array of variables with the output of get pages with those articles. If that's populated if there are any pages output, I create a section Code section menu. And within that I've got an H2 linking to my top level page, and then a list to all the other pages. So that's the code. Now at the moment, it won't actually output anything on your site, because you need to add either this function here into your template file, or you need to hook it to an action hook in your file. Which is what we will be doing in the next practicals. See you next time and thanks for watching.