Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
At the top of this chapter, I mentioned that one of the other differences between the public area. And the admin area is going to be the way that we handle what happens when a user clicks on a subject. We need to add a default subject behavior for the public. What I mean is, is that in the admin area, it's acceptable for the user to click on a subject and not get any content over on the right side. But I think that's not a great thing for our public. I think it's better if they actually get some content when they click on a subject. And to do that, what we'll do is we'll bring up the default page for the subject.
It seems to me that that default page ought to just be the first page, right? We should go ahead and just set that first page to be the default page. So, if you click on products, you ought to be seeing the content for large widgets. If you click on services, you ought to be seeing the content for retrofitting. Not just something about the subject generally here. Now we could have built our content management system differently so that every subject could have had its own blurb of text, but that's not what I'm after here. What I want is to just show you how we can have this default behavior built in. So, the place that I think we can add this is in our functions.php, and in our find_selected_page function that we already have, that's where we're determining what the subject and what the page is. So, if a subject is set, then we find that subject. What is the page in that case? Well it's null. But what if we went ahead and instead of just making it null, what if we went ahead and had it find a default page? Let's write a function, first of all, that'll find a default page for us, and then we can call it here. So, let's put it right here, above it.
Function, and we'll call it find the default_page_ for_subject. And that will take a subject id will be passed in. So, how can we find the first page of the subject? Well, the easiest way is we can find the list of all the pages for the subject. We know how to do that. We've got find pages for subject right here. Which also takes a subject ID. So, let's do that. Let's say page_set equals find pages for subject, subject_id.
And then, once we have that page set, then we know we want to get the first one out of there. So, it's the same thing that we did to find_page_by_id, right? Find_page_by_id executed a query and then it went ahead and it fetched the association and returned either the page or null. If it found one, then it returned it. Let's do the same thing here, I'm just going to actually copy this and we'll just use that and make it slightly different. Because we have, let's see, fetch association from page set and I'm just going to call this first page just so it's clear, if we find a first page, return it. Otherwise return nothing.
So, that'll do it for us. This function now will return a default page for the subject. So, given a subject id, we can find it. So, let's do that, let's say find_page_for_subject and then we can just pass in current subject id. See how that works? We find the subject by it's id. Now it's equal to current subject, then we ask that for it's id, and pass that in to find default page for the subject. Let's try it. Let's save it, let's come back over here.
This could become products. Okay, now wait a minute. We're not seeing anything different, we're still seeing our subject. That's because, even that we now got a page, we're not displaying it. We need to come back over here to index.php. And if we have a current subject, we've said manage subject. If we have a current page, then we've said display the page. Well, now, we don't want to actually have this in here at all anymore. If we have a current subject, we don't care. All we care about is, do we have a current page? If we have a current page then it will display.
So, that's it just take out that conditional from index.php, reload the page and there you go. See, click on services retrofitting pops up that's what that is right there. Our mission or our history but clicking just the subject on its own defaults so you can see it goes ahead and highlights it even. Because our highlighting and our navigation is all based on what we set in this find selected page function. So now at this point we're done and we have it working. However, I just want us to think about one other thing which is just that in the admin area. When we're clicking around on all these pages.
It still works. Everything works exactly as expected. Right? We're still seeing the, the subject, and that's because our managed contentpage.php still has this if we have a current subject. Right? So, it checks for that first, and it never gets to this second block down here for if we have a different page, right? Because that's different we can do both, however given the fact that we just got through learning about context. And how we can pass in different context I think we can make use of that here. Why do this extra MySQL query for all of those admin pages, right? We're doing some extra work every time we load a page, and we don't need to if we're in the admin area. We could have something that just checks to see if public. And so, if we're in the public context.
Then do that extra work. But if not, then don't. Go ahead, and just go back to our original behavior. Which was that the current page is equal to null. So, if we're in the public context, and then all we have to do is have something up here that says public. And by default I'm going to say that we are going to not be in the public context. That's different than what I was doing before where I was saying by default the public ought to be true, here I'm setting it by default to false. So, just be careful about that. And so then all I have to do is on my index.php.
In addition to asking for the selected page. I also have to pass in, true, for the fact that we are in the public side of things. And that will do that extra work for us. Let's go back and make sure we didn't break anything. As we click around we still get it. But when we're on the admin area, we don't make those extra MySQL calls because our context doesn't require it.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105445 Viewers
56 Video lessons · 117130 Viewers
71 Video lessons · 86354 Viewers
131 Video lessons · 41269 Viewers
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.