FREELessons: 12Length: 1.3 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.2 Identifying the Parent Page for a Query

In this lesson, you'll learn how to identify if the current page is a top-level page and, if not, which page is at the top of the hierarchy.

Related Links

2.2 Identifying the Parent Page for a Query

Hello and welcome back to this Tutsplus course on WordPress Plugin Development. In this part of the course we're gonna continue with our plugin to create a section menu in the sidebar, and we'll write a function that identifies which page we need to use as the parent page to list child pages of that page. Now this may sound a little bit counterintuitive. Why don't we just list the child pages of the current page? Imagine that your site has a structure a bit like this demo site I have here. The home page has no child pages. The About Us page does, it has to child pages, which we can't currently see here. The locations page also has a number of child pages, one for each location that this imaginary business operates from. So what I want to appear in my sidebar over here is if I'm in this main locations page, I want locations with a list of those pages below them. But if I'm in one of those specific locations pages such as New York, for example, I want the same list to appear. So the top level page in that case will be the page above the page I'm currently on. But if I'm on the main locations page the top level page will be the page to which I' am currently on. So what will do here is write the function that identifies that, so we can then send it to another function, to output the list of pages. So let's go into the plug in code. Here's our plug in, which we've already set up. And now, I'm gonna add some more commented out text, so I know exactly what I'm doing in each part of the code. So, that's right function. So that's our function name, and then within our function we need to start off by checking if we're on a page. And we do that by using the conditional function if is page. And if we're not on a page, the function will stop running and nothing will happen. So assuming we're on a page, the next thing to do is set up the global post variable. Because we'll be using that later on in our function. Now we need to check if this particular page has any parents. So going back to our site, this locations page doesn't have any parents. It's a top level page. Let me show you the pages in the back end. So you can see here I have locations, and then I have a number of pages beneath it. And I'd like to add comments all the way through my code. So I know exactly what's happening where when I come back to edited or if anybody else has to work with my code in the future. So we're using another if statement here. So if the post has a parent, this is a little bit confusing, this line, because you might think it means this post is a parent. But what this actually means, it's checking that this post has a parent post. So if it's a top level post, that won't be the case and it'll move onto the next bit of code but I'll show you in a moment. So if the post has a post parent what we need to do is fetch the IDs of all the posts that are parents of this post. But the specific ID we need is the very top level parent and we'll add that to a variable called Parent. So what we're doing here is we're creating an array called parent so we'll call that parents rather than parent. And within that array are the ID's of all of the ancestors of the current post. And the reason we're using array reverse, is because we want it to run in the opposite order to the way it would normally run, so normally it would run upwards. So the first value that would return would be the next post up. And then the second would be the next post above that and the final value would be the highest level post in the site. That's an ancestor of the current post. Now we want the first value to be the highest level because we have no way of knowing how many posts this will return. So we need to make sure we're fetching the right one. So we fetch the top level ancestor. And we return it. And what we're doing here is we're returning the zero value in this array, which is actually the first value that's fetched and that's the way arrays work is the first one is always called zero. So this will return the ID of the top level parent of the current post, if this post has a parent, but what if it doesn't? Well, in that case, we simply return the post ID of the current post. So, here's our full function. It checks if we're on a page. If so, it checks if this post has any parents. If that's the case, it finds the top level ancestor using array reverse get post ancestors and then returning this value in that array. And then if that isn't the case, and this is a top level post, it returns the ID of the current post. But at the moment, let me just fix that typo a bare check. At the moment, this doesn't add anything to our site it doesn't actually do anything. But what it does do is give us a value that we can use in our next function where we'll be outputting the list of posts. Which is what we'll be doing in the next part of the course. See you next time, and thanks for watching.

Back to the top