0

Right now I am using a different CMS, and I wrote a little bit of PHP which helps me (by using content from hidden pages) bring in a connected background and header and randomly display them, UNLESS the URL is one for a tag page.

Here's the code I use for my other CMS right now:

<?php $randarts = $this->find('/random-header-background/')->children(array());
$randart = array_rand($randarts, 1); 
$rand = $randarts[$randart]; ?>

This is combined with two other PHP calls to bring in the background and the header (which are inside of specific pages under the "Random Header Background" page set...

<?php if (url_match('/glorkian') OR url_match('/dino-run') OR url_match('/potato')){echo $this->content('background');}
    else {echo $rand->content('background');}
?>

and

 <?php if (url_match('/glorkian') OR url_match('/dino-run') OR url_match('/potato')){echo $this->content('header');}
  else {echo $rand->content();} ?>

Is there a way to do this in Wordpress? Or to randomly use backgrounds XYZ and if the background = X then use X header image, unless page (tag or category) = Y in which case you would always use background & header Y?

Does this make sense?

Check out http://pixeljam.com for an example (only the main page doesn't cache for this effect, the rest are cached but random except for the "Dino Run", "Glork" and "Potatoman" pages display their associated header & background.

Edit: OK, after some working on this I feel like I've come up with a solution, but it doesn't seem to work for some reason, maybe I can get some recommendations on this solution:

<?php $bg = array('dinorun.css','potatoman.css','glorkian.css','somethingelse.css','anothersomething.css');
    $randombg = mt_rand(0,3);
if (in_category('dino run')) { ?>
            <link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/dinorun.css">
<?php } elseif (in_category('potatoman')) { ?>
            <link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/potatoman.scss">
<?php } elseif (in_category(array('glorkian','glork'))) { ?>
            <link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/glorkian.css">
<?php } else { ?>
            <link rel="stylesheet" href="<?php bloginfo('stylesheet_directory') . $bg[$randombg]; ?>"
<?php } ?>`

This way I could just skip my in_category statements for the header and just change the css for the header background and the page background and header would always be matched.

  • You can do this using is_tag() & is_category() conditional tags for the header and background fairly easily.codex.wordpress.org/Conditional_Tags – Brad Dalton Jan 7 '14 at 19:25
  • I'm working on this, but I'm pretty far away from understanding how I can do this still. – ChaBuku Bakke Jan 7 '14 at 20:06
0

Here's some sample code you can use for the header or background. This uses the genesis header hook which you can change to a theme specific hook or WordPress hook.

add_action('genesis_header', 'conditional_header_image');

function conditional_header_image() {

if ( is_tag('your-tag')) {

echo '<div class="home-header"><img src="http://example.com/path/to/image.png" alt="home header image" /></div>';

elseif(is_page() )

echo '<div class="page-header"><img src="http://example.com/path/to/image.png" alt="page header image" /></div>';

    }
}

Untested.

I would look at the code in the Twenty Eleven theme to see how they use random headers and add conditionals to that.

  • Thanks, this really helps a lot. I tried to write something up, but after a while I just ended up at Codeacademy trying to learn more PHP! Hah. – ChaBuku Bakke Jan 7 '14 at 21:02
  • I don't actually think that theme uses random headers. The comments on the header.php for Twenty Eleven actually says that it's checking a post for a usable image and then if it's large enough it pulls that image in to the header. That is not what I am trying to do. – ChaBuku Bakke Jan 10 '14 at 0:44
  • It also has the option for random headers – Brad Dalton Jan 10 '14 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.