• 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 this 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; 
    
    }

    And that’s worked no problems.

    The trouble has now come when I’ve tried to split the returned content from this function into two separate divs. I believe I need to make the function into an array like you can get from wp_get_archives('echo=0'); but my PHP skills aren’t superb and I can’t quite figure out how to go about recreating the same kind of thing. It should look like:

    array(3) { [0]=> string(86) "January 2013"
                     [1]=> string(90) "September 2012"
                     [2]=> string(82) "March 2012"
    }

    Bearing in mind 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!

  • The topic ‘Split custom archives function into two columns’ is closed to new replies.