Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Let's say I try to click on something, I'll make sure that my Follow Links Continuously is set up, and if I go to about, you will see that the file is not found. Because what it's doing is it's going to the about path within the blog, not what we want at all. So to tackle this issue, we are going to need to integrate a little WordPress specific PHP code. The first area I want to address is the logo link.
This goes to the site home and not the blog home. So to handle this situation, we are going to need to bring in a custom function. Luckily they are pretty easy to write just basic PHP and even easier to implement. Now if you look at the custom theme folder, which we can do pretty easily by expanding the Files panel, you can see a file called functions.php. Unlike with the header.php and other related files like footer.php and index.php, we don't want to paste a copy of that file into our child theme folder.
Having the same functions twice breaks WordPress. So, what we need to do is to create a new PHP file, name it functions.php, and save it in our child theme folder. Then we can add any custom functions that we need to. So let's collapse the Files panel. I'll go to File > New, choose PHP under the Page Type, make sure None is selected under Layout, and click Create. Now I am going to go ahead and save that, and we want to save this in the Site Root, and we'll drill down to the blog, to wp-content > themes > roux, and we are going to name it functions.php, same name as the other file.
So I'll go to Code view, and we are going to strip out all of the HTML here. This is a pure PHP page. So I am going to save that one more time, so we have our file pristine and ready to go. Now, let's open the Code Snippet, which you'll be able to find in the exercise files, so I'll go to File > Open. So here's my custom function. Let me quickly walk you through the function so you know how to apply it. Function name is siteRoot. It brings in an argument called theFolder and then the first line sets up a variable home that uses a WordPress function that returns the entire blog URL.
Next, thePosition, another variable, is set to a string function called string pose or string position that finds where that argument is located in the string. So basically what we have here is the entire home URL, which is going to be something like http://localhostrouxacademy/blog, and it's going to find where the folder name, the folder string, which will be blog, is passed in and then the next line, code line 7, sets up another variable that extracts everything except for that particular passed in bit of string, the folder name, so we get just the first part of the path, which is exactly what we want. All right.
So let's copy this, move over to functions.php, paste it in, save the file, and I am going to go ahead and just close down these files. And now with these functions in place, we can actually go back to Dreamweaver and add the necessary code to the links. So we are going to be doing this in Code view in header.php. So let's scroll up a bit until we find the a tag that links the logo which is right here in the h1.
So I'll put in my opening code block and just to get rid of the little syntax error messages, let's close off that code block, and now I want to display the results of my function so I'll type in echo and then my function name, which is siteRoot and an opening and closing parentheses as well as the closing Semicolon. Now we want to pass in the path of the folder name that we are going to extract, and this is blog. All right.
So let me save the file, and we'll take a look and make sure that that's working. So I'll go into Design view, and I'll click Refresh here to make sure that I've got all the latest updates, and now I want to go ahead and make sure that when I click the logo here, it's going to go to the website homepage, not the blog homepage. Let's make sure that my Follow Link Continuously is selected, so it is. And when I hover over the logo and click on it once, it does in fact go to my homepage. So my function seems to be working properly. Now let's go back and finish off the coding.
I'll click on home here and then click on header and go to code. All right. Now since almost all of my main navigation links reference locations based on the site home, I can reuse this same PHP code. So I am going to go up, copy it quickly, and then place it in front of all of my links, except for one. So there is Programs, Admissions, Student Portal, Campus Portal, Alumni, and I am going to skip over the blog page and then do about. The one exception, of course, is that blog link.
Here I can take advantage of a WordPress function that provides the URL for where WordPress is stored within the blog site, and that function is site_url. In this case, the word site refers to the WordPress site, not the overall site. So I am going to put my cursor right in front of where it says blog/index and put in another PHP code block. We'll also want to use the echo term, and this time we are echoing that WordPress function site_url with an open and close parentheses, no arguments, and a closing Semicolon.
Now my last action is to adjust the path a little bit. Because it's pointing to the site_url, which is Roux Academy/blog, I don't want to leave the word blog here. So I'm going to remove that, and I am going to keep this trailing slash here. WordPress functions that return site locations always leave the trailing slash off, so you will need to add that in. My custom function siteRoot on the other hand includes the blog and only extracts whatever it is that you had included as an argument. Okay, we are ready to check the work.
So I am going to save our files and go back to Design view. Let's click Refresh to make sure we've got the latest version, and now I'll click on about, and we go to the about page, that's great. Let me go back a page, our logo link to home works, that's great, let's go back a page. Now let's just try out the blog link, which should just refresh this page. And there we are, perfect. In this lesson, you saw how to bring code in from your comp, both HTML and CSS rules to whip the header into shape.
You also got a chance to incorporate some WordPress PHP functions to make the links work correctly. Good job.
Get unlimited access to all courses for just $25/month.Become a member
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.