Help with PHP if-then..

2 replies
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
#ifthen #php
  • Profile picture of the author theIMgeek
    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
    Signature
    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
    {{ DiscussionBoard.errors[2704051].message }}
    • Profile picture of the author HenrikPoulsen
      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
      {{ DiscussionBoard.errors[2704083].message }}

Trending Topics