Saturday, Feb 17, 2007
This is a little customization I made to my site’s WordPress theme that I thought might be useful to others.
I wanted my site’s homepage, kolossus.com, to be my blog’s homepage, so that my most recent posts would show up there. No problem… But I also wanted my site’s homepage to have an introductory blurb at the top listing my most recent projects. The blurb is kinda big (I am partial to my mascot), and it really doesn’t belong on any page other than the site root.
The most logical way to only include a piece of content on your blog’s homepage is to use the built-in WordPress function “is_home()” in your theme’s index template. If is_home() returns true, you should be able to include your special homepage content and be done with it. But it’s not so simple — is_home() doesn’t only return true on your blog’s homepage — it also returns true if you’re paging through old posts (as in yourblog.com/page/2, yourblog.com/page/3, etc.). You can try it for yourself with the following PHP snippet:
if (is_home())
{
echo 'is_home() returned true';
}
else
{
echo 'is_home() returned false';
}
My friend Jason Winchell pointed out that I could access the PHP global variable $_SERVER[’REQUEST_URI’], and compare that to my blog’s homepage url. If they’re the same, I know we’re serving the site’s homepage, and I can include my special homepage blurb (encapsulated in its own template file, natch). The final PHP code that’s in my theme’s index.php file is below:
$request_loc = 'http://www.kolossus.com';
$request_loc .= $_SERVER['REQUEST_URI']; //request_uri is a relative url
$blog_loc = get_bloginfo('url');
$blog_loc .= '/'; //wordpress' blog url variable has no trailing slash, so add it
if ( $request_loc == $blog_loc )
{
include (TEMPLATEPATH . '/k_indexheader.php'); //special homepage header with blurb
}
else
{
get_header(); //standard header for all other blog pages
}
You should be able to use the same technique if, like me, you want to have some special content that shows up only on your blog’s homepage and nowhere else.
My site actually has two places where the most recent posts are listed — the site root, and a separate blog homepage that’s linked in my navbar. To get that second blog homepage to not show my intro blurb, I link to it as “/page/1″. This shows the same posts as my site root, but, obviously, the request url is different than the site root (”/”), so the above code still works to stop my intro blurb from being shown there.
|
|
|
I’ve been looking for a solution like this. I’m going to try it myself me thinks.
Thanks a lot for the tip!
Cheers!
Hey, that’s great. I’m glad this technique could be useful to someone else. Let me know if you have trouble getting it to work.
Thank you for your investigation, it is very usefull. But the last link is more simplier ))
THe right code should be
Yep, you’re right, that is a lot simpler than what I did. I wasn’t aware of the ‘is_paged()’ method till now. Thanks!