Changing your theme elements by using different php files

What if you need a slightly different header or sidebar in your posts from your pages?

I have been going through various reports to see how I could improve one of my sites. My goal is to improve user experience, and related to that goal is to improve navigation in the hopes that users will explore more pages. I like the idea of using css to change different parts of a website, but you may want to keep the basic look of your pages the same for branding purposes, and you may want only one element to be different. Creating a unique php file for your header, sidebar, footer, or post may be what you need.
    Here is a scenario: you have Google AdSense on your site, but you do not want every visitor to see it. A visitor who comes to your site through search is more likely to click on the ad, so you only want them to see it. Also, visitors hardly ever click on ads on your pages, but they do click on them when they are reading a post, so let’s elimanate them from the pages. You did find that ads in the header work best, so you want to keep it in the header. You also want the pages and the posts to maintain the same feeling.
    Here is the problem: WordPress uses the same php file (header.php) to create the header for a page or a post.
    The solution:
Step1: create a new header.php file
You will need a copy of the exisiting header.php file. You can either transfer from your site to your computer, or you can copy it from the editor box under Appearance>Editor from your WordPress dashboard. You will need a text editor, like Notepad++.
    Leave the exisitng header.php file for the pages. We will use this one for the posts. If you already have your Google AdSense code in the place that you want, you will only have to save this file as header1.php. To limit who sees these ads, you could use a plugin, but there is a wonderful function that will help (why add a plugin, when all you need is a bit of javascript). The site Scratch99 will explain this from a search engine code (the site has a good deal of information for you), so follow the instructions there (he goes over different methods). If you do not have the code in place, you could experiment to see where it works best, then add the javascript from the previously mentioned site. Make sure to save your changes.
Step 2: upload your changes
Use your FTP client to upload the header1.php file into your theme’s folder.
Step 3: edit the existing header.php
If you have the AdSense code here, the ads will be seen by visitors on the pages, so you can remove it, then save your changes by updating the file.
Step 4: update your single post file (usually single.php)
This file contains the elements which are displayed when a visitor is looking at a post. You will find the command <?php get_header(); ?>; this pulls up the header.php. What you want is the header1.php file, so you will need the following command   <?php include (TEMPLATEPATH . ‘/header1.php’); ?> to replace the old command. This brings up your new header file. Save your work.

That is it. Now a visitor looking at an individual post will see the ad, but no one else. How else may you use this? You could have different headers,sidebars, or footers for the various categories, or maybe you want the sidebar to contain certain information when a visitor is on a page from when they are on a post, such as a blogroll. There is a function in your WordPress admin panel that lets you choose which php file will determine the look of a post or page, so you could create new page.php and single.php files for different aspects of your site. Hope this gives you ideas.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Reply

Spam protection by WP Captcha-Free

Canonical URL by SEO No Duplicate WordPress Plugin