Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 9th Oct 2010, 09:42 AM   #1
HyperActive Warrior
War Room Member
 
HenrikPoulsen's Avatar
 
Join Date: 2009
Location: Denmark
Posts: 213
Thanks: 8
Thanked 5 Times in 5 Posts
Default
Help with PHP if-then..
Share on: 
fb share twitter share gplus share more share

Hey all

I'm trying to display seperate body classes for the home page and the inner pages.

I have written this into my index.php

<body class="<?php if (page-id-5){
echo "body-home";
} else {
echo "body-other";
}
?>">

It does work for the front page (page-id-5), but it just puts in "body-home" on the inner pages as well.

Any ideas?

Kind regards

AllRound Invoice - Small Business Invoicing
HenrikPoulsen is offline  
Unread 9th Oct 2010, 09:51 AM   #2
Peaceful Warrior
War Room Member
 
theIMgeek's Avatar
 
Join Date: 2009
Location: Calgary
Posts: 277
Thanks: 16
Thanked 106 Times in 63 Posts
Default
Re: Help with PHP if-then..
Share on: 
fb share twitter share gplus share more share

The trouble is that if (page-id-5) isn't a proper statement, so it always returns true.

Assuming that you're using Wordpress, the is_page() function oughtta do the trick.

Code:
if (is_page('5')){
  // do stuff for page with ID 5
} else {
  // do this for every other page
}
Wordpress also has a few other andy options like is_home_page(), if you're trying to change the front page of your site. The benefit there is that it will keep working even if you swap pages (with different IDs) Here's the complete list of these functions: Conditional Tags « WordPress Codex

-Ryan

FREE WSO: Protect and Automatically Deliver Your Digital Products

Ask the Internet Marketing Geek
<-- Happy to help with technical challenges
MiniSiteMaker.org <-- Free software to make your mini-sites fast and easy
theIMgeek is offline  
The Following User Says Thank You to theIMgeek For This Useful Post:
Unread 9th Oct 2010, 10:04 AM   #3
HyperActive Warrior
War Room Member
 
HenrikPoulsen's Avatar
 
Join Date: 2009
Location: Denmark
Posts: 213
Thanks: 8
Thanked 5 Times in 5 Posts
Default
Re: Help with PHP if-then..
Share on: 
fb share twitter share gplus share more share

Originally Posted by RJP View Post

The trouble is that if (page-id-5) isn't a proper statement, so it always returns true.

Assuming that you're using Wordpress, the is_page() function oughtta do the trick.

Code:
if (is_page('5')){
  // do stuff for page with ID 5
} else {
  // do this for every other page
}
Wordpress also has a few other andy options like is_home_page(), if you're trying to change the front page of your site. The benefit there is that it will keep working even if you swap pages (with different IDs) Here's the complete list of these functions: Conditional Tags « WordPress Codex

-Ryan

Thank you that did the trick

AllRound Invoice - Small Business Invoicing
HenrikPoulsen is offline  
Closed Thread


Bookmarks

Tags
ifthen, php


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 10:26 PM.