WordPress, you infuriate me sometimes!
What I’ve been trying to do for the past few hours is trying to have a way that I can display the Archive’s down the sidebar with a custom date format (without changing the global options in settings), a terribly convoluted task that should really be as simple as using the_date();. I just want to make January 2013 for example, Jan 13 instead while also having one year down one column and the other in the other.
I’ve managed to do the custom date formatting alright by adapting this code: http://www.wpbeginner.com/wp-themes/how-to-customize-the-display-of-wordpress-archives-in-your-sidebar/ to this:
function sidebar_archive_list($arclistno = "24") {
global $wpdb;
$limit = 0;
$year_prev = null;
$months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC");
foreach($months as $month) :
$year_current = $month->year;
if ($year_current != $year_prev){
if ($year_prev != null){ }
}
?>
<a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>" title="<?php echo date("F Y", mktime(0, 0, 0, $month->month, 1, $month->year)); ?>"><?php echo date("M y", mktime(0,0,0,$month->month,1,$month->year)); ?></a>
<?php
$year_prev = $year_current;
if(++$limit >= $arclistno) { break; }
endforeach;
}
That’s worked no problems.
The trouble has now come when I’ve tried to split the returned content from this function into the two separate column divs. I believe I need to make the function into an array like you can get from wp_get_archives(‘echo=0’); (and some extra code) but my PHP skills aren’t superb and I can’t quite figure out how to go about recreating the same kind of thing. I think it should look something like:
array(3) { [0]=> string(86) "January 2013"
[1]=> string(90) "September 2012"
[2]=> string(82) "March 2012"
}
When var_dumped. Also it’s worth bearing in mind for whatever reason the anchor links also surround the dates, but echo, rather than print out.
The code I’m using for the content split is:
<?php
$links = print(sidebar_archive_list());
$archive_n = count($links);
for ($i=0;$i<$archive_n;$i++):
if ($i<$archive_n/2):
$archive_left = $archive_left.'<li>'.$links[$i].'</li>';
elseif ($i>=$archive_n/2):
$archive_right = $archive_right.'<li>'.$links[$i].'</li>';
endif;
endfor;
?>
<div class="group">
<ul class="sb_double_column_list alignleft">
<?php echo $archive_left;?>
</ul>
<ul class="sb_double_column_list alignright">
<?php echo $archive_right;?>
</ul>
</div>
Any help or advice would be wholeheartedly appreciated. I’m in a real rut with this!
Okay after messing around a bit I managed to modify wp_get_archives into my own custom function which allowed me to pass it through the column creating code in a similar manner to how I did when trying to use wp_get_archives on its own.
The code is as follows.
Just drop it into the
functions.phpand then use thesidebar_archive_list(). Please note that the function only supports the monthly display and I’ve altered the defaults slightly fromwp_get_archives. So it will return the array rather than echoing it and the default limit is 24 (12 months down one column and vice versa).