0
votes

I have a navigation in WordPress which is set up to show the default monthly archives, which is this case each month is equal to an Issue – like a magazine.

Currently I am using the wp_get_archives() to echo the month title as a list item in my nav, which works fine.

What I want to do is prepend an issue number, which increments by 1 each month, to the month name in the list item.

At the moment I have tried creating a function in my theme's functions file:

function is_new_month() {
    global $currentmonth, $previousmonth;
    if ( $currentmonth != $previousmonth )
        return 1;
    else
        return 0;
}

I've simply based this off the WP core is_new_day function but I'm not convinced this will work.

Then in my header I've added:

<?php

$startissue = 1;

if (is_new_month()): 
    $startissue++;
endif; ?>

<ul>
    <li>Issues
        <ul>
            <?php wp_get_archives('before=Issue ' . $startissue . ' :') ?>
        </ul>
    </li>
</ul>

So my end result should show (example showing last 4 issues / months):

Issue 4: January 2014
Issue 3: December 2013
Issue 2: November 2013
Issue 1: October 2013

Each will link to a monthly archive of posts.

I think my custom function is fundamentally flawed. Is there a better way to do this?


That works a treat, Dima, thank you. Except it shows the results backwards, ie:

Issue 1: January 2014
Issue 2: December 2013
Issue 3: November 2013
Issue 4: October 2013

And not:

Issue 4: January 2014
Issue 3: December 2013
Issue 2: November 2013
Issue 1: October 2013

So I added in the order parameter into here:

$archives = wp_get_archives(array('echo'=>false, 'before'=>'Issue %num%: ', 'order'=>'ASC'));

Which works ok but still not 100%:

Issue 1: October 2014
Issue 2: November 2013
Issue 3: December 2013
Issue 4: January 2013

Very close, however, so thank you.

1
You should replace $issue_offset++ with $issue_offset--. And set issue_offset to appropriate starting value: $issue_offset = 4;.Dima Knivets
Thanks for your help, Dima.Steve Perry

1 Answers

0
votes

Try this (put this within ul tag):

$issue_offset = 1;
$archives = wp_get_archives(array('echo'=>false, 'before'=>'Issue %num%: '));
$archives = explode( '</li>' , $archives );
$links = array();
foreach( $archives as $link ) {
    $link = str_replace( array( '<li>' , "\n" , "\t" , "\s" ), '' , $link );
    $link = str_replace('%num%', $issue_offset, $link);
    if( '' != $link ){
        $links[] = $link;
        $issue_offset++;
    }
    else {
        continue;
    }
}
foreach($links as $archive){
    echo "<li>$archive</li>";
}