WordPress.org

Support

Support » How-To and Troubleshooting » Problem with is_page() and other php

Problem with is_page() and other php

  • I’ve added this at the top of my page.php template so I can use different color schemes on my pages:

    <?php

    $pagecolor=”;

    $pagecolors = array(
    ‘about-me’ => ‘green’,
    ‘About Me’ => ‘green’,
    ‘my-bio’ => ‘green’,
    ‘contact’ => ‘green’,
    ‘gb-view’ => ‘orange’,
    ‘gb-sign’ => ‘orange’,
    ‘gallery’ => ‘yellow’,
    ‘links’ => ‘blue’,
    ‘story-excerpts’ => ‘green’,
    );

    foreach ($pagecolors as $slug => $color) {
    if(is_page(‘$slug’)) {
    $pagecolor = $color;
    }
    }
    if ($pagecolor = ”) {
    $pagecolor = ‘purple’;
    echo ‘Warning: default color used’;
    }

    include (TEMPLATEPATH . “/header-$pagecolor.php”); ?>

    <body background=”<?php bloginfo(‘template_url’); ?>/images/background_$pagecolor.png”>

    <div id=”textColumn”>

    <img class=”topBanner” src=”<?php bloginfo(‘template_url’); ?>/images/top_$pagecolor.png”>

    But I get errors that the system is not getting the right header & image files, which means the system is not getting the color from $pagecolor. Not even my if statement is working to give a default color.
    I’m new to php and I’m probably doing something obviously wrong. Someone please help me!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hmmm, I’m still working on this, and I think I found one problem and fixed it, but still the colors aren’t working.

    I fixed the $pagecolor in the background and topBanner images, so instead of something like:

    top_$pagecolor.png

    I’ve got:

    top_<?php $pagecolor; ?>.png

    So those two little oopsies are fixed, BUT the header file is still not getting the right color.

    You want:

    <?php echo $pagecolor; ?>

    Otherwise the value of $pagecolor is not displayed.

    Thanks for catching that, that fixes the body background and topBanner images.

    But I don’t think that’s the problem with the header. It’s using the include function to get the header template file. Could it be a problem with using the right quotes? I get single & double quotes confused in php. Also, could it be something about declaring the $pagecolor variable? It doesn’t show in my example above, but I tried adding

    global $pagecolor;

    at the top, but it didn’t change anything. This is what I’m getting:

    Warning: main(C:\xampp\htdocs\wp/wp-content/themes/my-personal-notebook/header-$pagecolor.php) [function.main]: failed to open stream: No such file or directory in C:\xampp\htdocs\wp\wp-content\themes\my-personal-notebook\page.php on line 27

    Warning: main() [function.include]: Failed opening ‘C:\xampp\htdocs\wp/wp-content/themes/my-personal-notebook/header-$pagecolor.php’ for inclusion (include_path=’.;\xampp\php\pear\’) in C:\xampp\htdocs\wp\wp-content\themes\my-personal-notebook\page.php on line 27

    You’ve got the double-quotes correct in your include above, so I’m surprised it’s not working. But go ahead and try this:

    include (TEMPLATEPATH . "/header-" . $pagecolor . ".php");

    Thanks for the help, but it’s still not happy.

    To be completely clear exactly what’s in my file right now, I’ll repeat it again:

    <?php
    global $pagecolor;
    $pagecolor=”;

    $pagecolors = array(
    ‘about-me’ => ‘green’,
    ‘About Me’ => ‘green’,
    ‘my-bio’ => ‘green’,
    ‘contact’ => ‘green’,
    ‘gb-view’ => ‘orange’,
    ‘gb-sign’ => ‘orange’,
    ‘gallery’ => ‘yellow’,
    ‘links’ => ‘blue’,
    ‘story-excerpts’ => ‘green’,
    );

    foreach ($pagecolors as $slug => $color) {
    if(is_page(‘$slug’)) {
    $pagecolor = $color;
    }
    }
    if ($pagecolor = ”) {
    $pagecolor = ‘purple’;
    echo ‘Warning: default color used’;
    }

    include (TEMPLATEPATH . “/header-” . $pagecolor . “.php”); ?>

    <body background=”<?php bloginfo(‘template_url’); ?>/images/background_<?php echo $pagecolor; ?>.png”>

    <div id=”textColumn”>

    <img class=”topBanner” src=”<?php bloginfo(‘template_url’); ?>/images/top_<?php echo $pagecolor; ?>.png”>

    and I’m getting the error:

    Warning: main(C:\xampp\htdocs\wp/wp-content/themes/my-personal-notebook/header-.php) [function.main]: failed to open stream: No such file or directory in C:\xampp\htdocs\wp\wp-content\themes\my-personal-notebook\page.php on line 27

    Warning: main() [function.include]: Failed opening ‘C:\xampp\htdocs\wp/wp-content/themes/my-personal-notebook/header-.php’ for inclusion (include_path=’.;\xampp\php\pear\’) in C:\xampp\htdocs\wp\wp-content\themes\my-personal-notebook\page.php on line 27

    Urgh! the warning’s have gone off the edge. I’ll clean it up the directory structure:

    Warning: main(C:\directory/header-.php) [function.main]: failed to open stream: No such file or directory in C:\directory\page.php on line 27

    Warning: main() [function.include]: Failed opening ‘C:\directory/header-.php’ for inclusion (include_path=’.;\xampp\php\pear\’) in C:\directory\page.php on line 27

    Looking closer…

    Ok, lines to change:

    if(is_page('$slug')) {
    ~should be:
    if(is_page("$slug")) {

    if ($pagecolor = '') {
    ~should be:
    if ($pagecolor == '') {

    It works! It works!

    Thanks so much for your help! Thank you! *kissing your feet* *splutter*

    Hey, new shoes… :)

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Problem with is_page() and other php’ is closed to new replies.