Thesis Tutorial: How to Conditionally Change Content

In yesterday’s post, we spoke about why you would want to change your content based on traffic intent. In today’s post, I’m going to give you a basic framework about how to do it. This post is written as a Thesis Tutorial, because working with Thesis is just easier (see my Thesis review), but you can easily adapt the code to any website or theme.

OK. Like all Thesis customizations, we’re going to need to open up the custom_functions.php file. In this example, we are going to offer different social buttons based on where the user came from. If they came from a social site, we’ll show the interactive buttons with counts/votes. If they came from anyplace else, we’re going to show static graphic icon buttons.

For the website I’m using I put the buttons under the author byline, so my code will go in that function (if you are going to copy and paste, wait until the end for the final code so you get all the semicolons and parentheses).

//this is author byline
function uauthor_byline()

I’m going to make sure the buttons only appear on single pages so I’ll need the following bit of code:

if (is_single())

Ok now we get to the programming. We’ll need two variables and one array. The variables will hold the referring URL and a flag that tells whether that condition is true, and the array will hold the list of sites we are checking against.

$CUsocial = false; // this is the flag for social traffic
$CUref = $_SERVER['HTTP_REFERER']; //this is the referring URL
$CUsocialar = array('reddit', 'stumbleupon', 'digg', 'twitter', 'facebook', 'delicious'); // this is an array of social sites

So what we want to do next is take the list of social sites and see if any of them are in the referer. If they are, we set the flag to true.

//check all the social sites
$i=0;while ($i<=count($CUsocialar)){
if(stristr($CUref, $CUsocialar[$i])) {
$CUsocial = true;
}
$i++;
}

Now, if you’re a stickler, you could make the case that, if Twitter was in the filename and not the domain, we could get a false positive, and you would be correct. I just don’t think that’s going to happen often enough to be a real concern. OK so now we know whether the referring site is any of the social websites we want to trap for. If it is, the $CUsocial variable will be ‘true’ so we’ll need this bit of code:

if ($CUsocial){
//code for active social websites goes here
}else{
//code for default condition goes here
?>}

The code above has a placeholder for the buttons you can get from places like digg, stumbleupon and facebook. Since there are so many tutorials and instructions from the original websites, I just left placeholders. Here’s the full code:

function uauthor_byline() {
if (is_single()){
$CUsocial = false; // this is the flag for social traffic
$CUref = $_SERVER['HTTP_REFERER']; //this is the referring URL
$CUsocialar = array('reddit', 'stumbleupon', 'digg', 'twitter', 'facebook', 'delicious'); // this is an array of social sites
//check all the social sites
$i=0;while ($i<=count($CUsocialar)){ if(stristr($CUref, $CUsocialar[$i])) { $CUsocial = true; } $i++; }
if ($CUsocial){
//code for active social websites goes here
}else{
//code for default condition goes here
}
}

The following is just a starting point and can be re-used and expanded upon. For example, if you want to trap for search engines, here’s the extra code you would need:

$CUsearch = false; //this is the flag for search traffic
$CUsearchar = array('google', 'yahoo', 'bing'); //this is an array of search sites

//check all the search sites
$i=0;while ($i<=count($CUsocialar)){
if(stristr($CUref, $CUsearchar[$i])) {
$CUsearch = true;
}
$i++;
}

But you can use the code all over the site content, header, sidebar, etc. You can combine it with date based triggers, or there are many, many different possibilities, if you spend time playing with the code.
photo credit: The U.S. Army

GraywolfSEO.com runs on the Genesis Framework

Genesis Framework

Genesis lets you to quickly and easily build amazing websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.
It's that simple - start using Genesis now!


Take advantage of the 6 default layout options, comprehensive SEO settings, rock-solid security, flexible theme options, cool custom widgets, custom design hooks, and a huge selection of child themes ("skins") that make your site look the way you want it to. With automatic theme updates and world-class support included, Genesis is the smart choice for your WordPress website or blog.


tla starter kit

Advertisers:

  1. Text Link Ads - New customers can get $100 in free text links.
  2. BOTW.org - Get a premier listing in the internet's oldest directory.
  3. Need an SEO Audit for your website, look at my SEO Consulting Services
  4. TigerTech - Great Web Hosting service at a great price.