if is home with wordpress

Discussion in 'PHP' started by Hades, Oct 21, 2008.

  1. #1
    Hey Guys,

    I need a bit of help since im completely lost when it comes to php.

    What I am trying to do is set it so that if the page is the home/front page, it would have a certain option, if it's single.php, it would have a different one, if it's page.php a different one, and so on.

    Basically, I'm trying to apply this to the body selector in the stylesheet so that I can change the background color for different pages, (otherwise the site would be messed up.

    Anyone who can help, I would greatly appreciate it.

    Regards,
    Nick
     
    Hades, Oct 21, 2008 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    If you look in the default theme's header.php you should see what you need.
    
    // Checks to see whether it needs a sidebar or not
    if ( !empty($withcomments) && !is_single() ) {
    ?>
    	#page { background: url("<?php bloginfo('stylesheet_directory'); ?>/images/kubrickbg-<?php bloginfo('text_direction'); ?>.jpg") repeat-y top; border: none; }
    
    PHP:
    change the if to if(is_home() ) {

    and adjust the CSS as needed.
     
    shallowink, Oct 21, 2008 IP
  3. Hades

    Hades Well-Known Member

    Messages:
    1,873
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    150
    #3
    Hey Shallowink,

    Maybe you can take a look at what I have? For some reason it only shows the "else" part, and doesn't understand the other ones. Am I doing something wrong?

    
    body { background : #<?php 
    if (is_home()) {
    echo "000"; }
    if (is_front_page()) {
    echo "000"; }
    elseif (is_single() && is_page()) {
    echo "fff"; }
    else {
    echo "555"; } ?>  url(images/bg.png) repeat-x; font : normal 0.625em/1em Arial, Helvetica, sans-serif; }
    PHP:
    Thanks,
    Nick
     
    Hades, Oct 21, 2008 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    I just tested it and your code works. Do you still have the background color defined in an external stylesheet?

    The is_home and is_front_page are pretty much the same thing, unless you are using a static page for the front page. Doesn't hurt to have it twice but just so you know. Now the elseif(is_single() && is_page() ) will never work since the && is saying this is a single post AND it is a page. Guessing you want it to be either OR ? If so use || in place of &&. If you keep the is_front_page() test, I would change it to elseif. Mainly cause the first IF is being overridden by the IF, ELSEIF, ELSE.

    Here's the exact code from the test header.php I'm using...

    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head profile="http://gmpg.org/xfn/11">
    <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
    
    <title><?php bloginfo('name'); ?> <?php if ( is_single() ) { ?> &raquo; Blog Archive <?php } ?> <?php wp_title(); ?></title>
    
    <meta name="generator" content="WordPress <?php bloginfo('version'); ?>" /> <!-- leave this for stats -->
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
    <link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
    <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
    
    <style type="text/css" media="screen">
    
    body { background : #<?php 
    if (is_home()) {
    echo "000"; }
    elseif (is_front_page()) {
    echo "000"; }
    elseif (is_single() || is_page()) {
    echo "fff"; }
    else {
    echo "555"; } ?>  url(images/bg.png) repeat-x; font : normal 0.625em/1em Arial, Helvetica, sans-serif; 
    
    </style>
    
    <?php wp_head(); ?>
    </head>
    <body>
    
    PHP:
     
    shallowink, Oct 21, 2008 IP