13

May

Quick Tip: WordPress Nav Link to External Site

Quick Tip for WordPressBeing that this site is built using WordPress, there are always little tweaks, hacks and plugins I am searching for to massage the framework to do what I want it to do. I found a pretty simple little hack that I figured would be useful to just about anyone who uses WordPress and thought I would share. The object of this change is to make one of your top level navigation items link to an external site, outside of your WordPress site.

So if you’ve spent any time at all working on a WordPress site, it would stand to reason that you have created a new page before. This can be achieved by clicking on the “Add New” link under the “Pages” section in the left nav of the control panel. Creating a new page will add a new main navigation item with the title matching the name you gave to the new page. This is all fine and good, but what if you want to add a navigation item that links to a page or site outside the current WordPress site? Here’s how you can do it…

First we will create a new page template. Open a text editor app and save a file, which is still blank, with the name of your choosing. The name you choose should most likely relate to what page or site you will be linking to, so it will make sense to you later. For my site, I wanted the main nav item to link to my portfolio, so i named the file portpage.php. Make sure you use the .php extension.

Next we will add the necessary code to the file you have just saved. Simply copy and paste the following code into the file (exactly as it is here) and save the file once more.

<?php
/*
Template Name: mywebsite
*/
?>
<?php header('Location: http://www.mywebsite.com');
      die();
?>

Now we will make a couple changes to the code we just pasted in the file. Where you see Template Name: you will change “mywebsite” to the name you would like to use for your template. Again, for mine I used portpage. This is the name that will show up in the WordPress interface when you create your page. The other change is to the Location. Change “http://www.mywebsite.com” to the URL you wish the nav item to link to. Don’t forget to save your changes!

Once the template is complete and saved, we need to upload it to the server. Locate the directory for your template files. An example of the location is: [Your Domain]/wp-content/themes/[Your Theme]. Upload the template file to this directory and you are ready to put it into action.

Log into the panel of your WordPress site and click on “Add New” under the “Pages” section. Name the page whatever you would like the link to say. The body of the page can be left blank. Now on the right-hand side, under the “Page Attributes” section, change the template from “Default Template” to the name that you gave your new template. If your template does not show up in the select menu, you most likely failed to complete one of the previous steps.

So there you go. A link in your main navigation that links somewhere outside of your WordPress site. Hope this was helpful!

Comments are closed.

Recent Posts

Brining Beer to Life
Aug, 18

Future of the Web
May, 24

Flickr

Santa Barbara 2011Santa Barbara 2011