Post image for 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.
Creative Commons License photo credit: The U.S. Army

This post originally came from Michael Gray who is an SEO Consultant. Be sure not to miss the Thesis WordPress Theme review.

Thesis Tutorial: How to Conditionally Change Content

tla starter kit

Related posts:

  1. Thesis Tutorial – Adding Date Based Triggers to Your Posts There are a lot of times when you are working…
  2. Change Your Content Based on Traffic Intent A few weeks ago, Brent Payne made a post about…
  3. Thesis Tutorial – How to Add Adsense Section Targeting Using Adsense on your blog usually isn’t the most profitable…
  4. Make Thesis Work Better With Digg and Facebook If you’re involved with social media sites like Digg, Facebbok…
  5. How to Add a Carousel to Your Thesis Blog If you’ve spent any time visiting blogs lately chances are…

Advertisers:

  1. Text Link Ads – New customers can get 0 in free text links.
  2. BOTW.org – Get a premier listing in the internet’s oldest directory.
  3. Ezilon.com Regional Directory – Check to see if your website is listed!
  4. Page1Hosting – Class C IP Hosting starting at .99.
  5. Directory Journal – List your website in our growing web directory today.
  6. Content Customs – Unique and high quality SEO writing services, providing webmasters with hundreds of SEO articles per week
  7. Majestic SEO – Competitive back link intellegence for SEO Analysis
  8. Glass Whiteboards – For a professional durable white board with no ghosting, streaking or marker stains, see my Glass Whiteboard Review
  9. Need an SEO Audit for your website, look at my SEO Consulting Services
  10. KnowEm – Protect your brand, product or company name with a continually growing list of social media sites.
  11. Scribe SEO Review find out how to better optimize your wordpress posts.
  12. TigerTech – Great Web Hosting service at a great price.

Michael Gray – Graywolf’s SEO Blog

0 Comments to "Thesis Tutorial: How to Conditionally Change Content"

Would you like to share your thoughts?