• skernick

    (@skernick)


    Hello. I am a beginner at this and have run into a problem. I want to have a unique banner appear at the top of each months posts. I have the theme heavily Frankesteined and I am afraid I have hit a wall. The site is here. I have the banner set up for November but as you can see, it sticks for every month. Any help would be havily appreciated. Thanks. -Sean

Viewing 3 replies - 1 through 3 (of 3 total)
  • Kafkaesqui

    (@kafkaesqui)

    I have the theme heavily Frankesteined

    Hehe.

    Here’s a little PHP to automate the image; place it somewhere before the image is displayed in your template:

    <?php $cur_month = strtolower(date('F')); ?>

    Then alter your ‘top banner’ code to:

    <div class="topbanner2"><img src="https://www.yankelovich.com/blog/images/<?php echo $cur_month; ?>/<?php echo $cur_month; ?>.jpg" border="0"></div>

    References:
    https://php.net/date
    https://php.net/strtolower

    Thread Starter skernick

    (@skernick)

    Thanks so much for the reply. I am almost a complete novice with php. This works great but it misses my target a little. The banner image will change as the month does but what I need it to do is change when the user clicks on an archived month. This way the banner matches the month that they are viewing. If you have any more assistance it would be incredibly helpful. Thanks so much.

    what I need it to do is change when the user clicks on an archived month.

    You can get as complicated as you want with this. And if you don’t know PHP it will get complicated. But just trust me…

    The only part we need to change is the initial one I provided:

    <?php $cur_month = strtolower(date('F')); ?>

    This is going to expand bit so we can properly decide what to assign $cur_month. The following hits *current* on the home page, but the archive’s month on archived query pages:

    <?php
    global $month, $wp_query;
    if( is_archive() ) {
    	$cur_month = strtolower($month[$wp_query->query['monthnum']]);
    } else {
    	$cur_month = strtolower(date('F'));
    }
    ?>

    Let’s go over what’s happening here:

    is_archive() is a WordPress conditional tag that returns true if we’re on an archive page (i.e. posts for November 2007). When we are, we can access the archive month through $wp_query->query['monthnum'] — however it’s only the numeric value, so we also have to pass it as an array key to the global variable $month (which hold the names of the months).

    If we’re not on an archive page, the default is to use the current date (‘F’ is a format character that tells it to only return the full text name for the month), as before.

    For both, strtolower() is a PHP function which just returns any string it’s passed in all lowercase characters.

    Now, if you’d like this to work with the current month, the archive’s month, and the month for a post when on a post’s permalink page:

    <?php
    global $month, $wp_query;
    if( is_archive() ) {
    	$cur_month = strtolower($month[$wp_query->query['monthnum']]);
    } elseif( is_single() ) {
    	$cur_month = strtolower($month[substr($wp_query->post->post_date, 5, 2)]);
    } else {
    	$cur_month = strtolower(date('F'));
    }
    ?>

    The first and last if blocks are the same. The one we perform for is_single() (single post page) does something similar to the archive month, but collects the numeric month from the $post object’s post_date property (substr() is another PHP function, this one letting us pick out the month from the post_date’s format of YYYY-MM-DD HH:MM:SS).

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Changing theme each month.’ is closed to new replies.