WordPress.org

Ready to get started?Download WordPress

Forums

Problem with is_page() and other php (10 posts)

  1. tkhaj
    Member
    Posted 8 years ago #

    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!

  2. tkhaj
    Member
    Posted 8 years ago #

    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.

  3. Kafkaesqui

    Posted 8 years ago #

    You want:

    <?php echo $pagecolor; ?>

    Otherwise the value of $pagecolor is not displayed.

  4. tkhaj
    Member
    Posted 8 years ago #

    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

  5. Kafkaesqui

    Posted 8 years ago #

    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");

  6. tkhaj
    Member
    Posted 8 years ago #

    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

  7. tkhaj
    Member
    Posted 8 years ago #

    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

  8. Kafkaesqui

    Posted 8 years ago #

    Looking closer...

    Ok, lines to change:

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

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

  9. tkhaj
    Member
    Posted 8 years ago #

    It works! It works!

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

  10. Kafkaesqui

    Posted 8 years ago #

    Hey, new shoes... :)

Topic Closed

This topic has been closed to new replies.

About this Topic