bioneural.net site preferences

Accessibility

Toggle width/ text size:

style

Default/Alternate

Suits visual impairment, mobile devices

Styling

Change the theme:

layout

Sorry, this option is not enabled

Link behaviour

Links with an icon are off-site:

links

Right-click any link to optionally open in a new window or tab


 

Manageable WordPress archive lists

If you've been blogging for a while, chances are your list of post archives by month is growing long in the tooth. Thankfully—once you know how—WordPress makes doing something about it pretty easy (a characteristic I've come to appreciate). I had tried to fumble my way to coding a drop-down menu for my archives but my initial efforts were unsuccessful. I then discovered that the solution was right in front of me all along. RTFM.

What are we hoping to achieve?

Let a picture tell the story:

Archives

Code replacement

My site is built on the K2 WP modification (r163), which lists archives by month in both page-archives.php and sidebar.php. The original code outputs a simple unordered list (above left) and looks like this:

<div class="sb-months"><h2><?php _e('Archives by month'); ?></h2>
	<ul>
		<?php wp_get_archives('type=monthly'); ?>
	</ul>
</div>

We can replace it with this (output as above, right):

<div class="sb-months"><h2><?php _e('Archives by month'); ?></h2>
	<p>Please select a month:</p>
	<noscript><p class="alert">Please enable JavaScript for full functionality</p></noscript>
	<select name="archivemenu" onchange="document.location.href=this.options[this.selectedIndex].value;">
		<?php get_archives('monthly', '', 'option', '', '', 'TRUE'); ?>
	</select>
</div>

This solution does require JavaScript to be enabled and is modified from the example given in the WP Codex here (the code as listed currently is invalid XHTML and generates a PHP parsing error). It is valid XHTML and works with Safari, Firefox, and IE 6 and 7.

8 responses to Manageable WordPress archive lists


  1. 1 Wayne

    Nice, very nice, no need for a plugin or anything. Thanks for the code, I am using it on my archives page also. Keep up the good work. :)

  2. 2 Bruce

    All you need now Wayne is a long list of archives to show it off ;-)

    The code isn't mine; as I said it's originally from the Codex—I just modified it to make it valid XHTML (and to work!).

  3. 3 gigi

    Dear Bruce,
    hot to get the same thing listing Categories ?

    Thanks

    Gigi

  4. 4 Bruce

    Gigi, try wp_list_categories() using the parameters listed here. If you're not using WP 2.1+, search the Codex for the older function.

  5. 5 gigi

    Bruce,
    i'm using this, but it shows only an empty menu:

    Please select a category:
    Please enable JavaScript for full functionality

    Where is the problem ?

    Gigi

  6. 6 Bruce

    Sorry Gigi I'm not running WP 2.1 so can't test this. I tried the depreciated functions but couldn't get it working. I suggest trying the WP forums; looks like this question has been asked there recently (but not solved as yet).

  7. 7 gigi

    Thanks.

    Did you notice how these two site achieve it ?

    http://www.nomad4ever.com/

    http://www.gobackpacking.com/Blog

  8. 8 Bruce

    Gigi you can't see what PHP functions are being called by viewing the page source. Perhaps try asking the webmaster?

Something to say?

Comments may be moderated, are subject to spam filtering, and should be inoffensive and relevant to this post. Please disclose commercial interests.

Usable tags include <a href=""> <blockquote> <em>.