Help with PHP if-then..

by 2 replies
3
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
#programming #ifthen #php
  • 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
    • [ 1 ] Thanks
    • [1] reply

Next Topics on Trending Feed