Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Help with PHP if-then.. (https://www.warriorforum.com/programming/270798-help-php-if-then.html)

HenrikPoulsen 9th October 2010 09:42 AM

Help with PHP if-then..
 
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

theIMgeek 9th October 2010 09:51 AM

Re: Help with PHP if-then..
 
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

HenrikPoulsen 9th October 2010 10:04 AM

Re: Help with PHP if-then..
 
Quote:

Originally Posted by RJP (Post 2704051)
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 :)


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