WordPress.org

Ready to get started?Download WordPress

Forums

wp_get_archives year rewriting help (6 posts)

  1. humbucker
    Member
    Posted 5 years ago #

    Hello,

    I'm having a little problem trying to rewrite/reformat the year in my archives.

    For now it's listing like January 2009
    and I d'like to display it like January '09

    Moreover, is it possible to add a separate symbol between every months like : December '09 / January '09 / .....

    Big big thanks if you can help.

    M.

  2. Austin Matzko
    Member
    Posted 5 years ago #

    Here's one way to to do it. Replace wp_get_archives() with the following:

    echo preg_replace(
    	'#(' . implode('|', array_values($GLOBALS['wp_locale']->month)) . ')\s\d{2}(\d{2})#',
    	'$1 ’$2 /',
    	wp_get_archives('echo=0')
    );
  3. humbucker
    Member
    Posted 5 years ago #

    Hello, huge thanks to you, you made my day man, can I ask something more so that I learn ?
    Can you maybe explain me what you did because that code seems so strange to me...
    Usually I understand the common wp php syntaxes but I gotta say I'm kinda stuck and it's always nice to know.
    Thank you!

  4. Austin Matzko
    Member
    Posted 5 years ago #

    implode('|', array_values($GLOBALS['wp_locale']->month))

    That basically makes a string of all the months, like so:

    January|February|March...

    preg_replace is a function that looks for a pattern---called a "regular expression"---and replaces it with something else. In this case, it looks for a month pattern (from that string above) followed by a four-digit number (presumably a year) and then replaces it with the month followed by the right single quote and the last two digits of the year.

    In wp_get_archives('echo=0') the echo=0 argument basically just tells wp_get_archives to return its string instead of printing it; that way we can use it in preg_replace.

  5. lightblack
    Member
    Posted 4 years ago #

    Hello,

    how would I go about changing the month to the abbreviated form in this rewrite in my archives.
    I'd like to display it like Jan '09
    I just don't know what I'm missing. Help would be really appreciated.

    J.

  6. Keetee
    Member
    Posted 4 years ago #

    I just made a post on my blog about how to edit date formatting for wp_get_archives. Should answer your question, lightback. It involves going into wp-includes which isn't ideal due to WP updates, but it's probably less "messy" than the theme alternatives. Hope this helps!

Topic Closed

This topic has been closed to new replies.

About this Topic