WordPress.org

Ready to get started?Download WordPress

Forums

Archives by YEAR (this is different!) (33 posts)

  1. laurenf
    Member
    Posted 8 years ago #

    I want to list my archives by year, so that instead of:
    July 2005
    June 2005
    May 2005
    etc...

    I get:
    2005
    2004
    2003
    etc...

    I've read all the other forum posts on this, but I'm not interested in plugins that will make my archives look like this:
    2005 May
    June
    July
    2004 December
    November

    etc.

    I just want by YEAR.

    Also, I don't know a lot about php, but why can't this tag:

    <?php wp_get_archives('type=monthly'); ?>

    be changed to type=yearly ?? Why does "weekly" work, or "postbypost" by not yearly?

    Any help would be GREATLY appreciated! Thank you.

  2. Kafkaesqui

    Posted 8 years ago #

    "Why does "weekly" work, or "postbypost" by not yearly?"

    Because they didn't anticipate you'd come along and ask for it? :)

    Here's a bit of code that does what you're after:

    <ul>
    <li>Archives</li>
    <li><ul>
    <?php
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date");
    foreach($years as $year) : ?>
    <li><a href="<?php echo get_year_link($year); ?> "><?php echo $year; ?></a></li>
    <?php endforeach; ?>
    </ul></li>
    </ul>

    You can of course change the html format used.

  3. Beel
    Member
    Posted 8 years ago #

    Unless I had about 20 years of posts, I'd just throw out hard-coded links and once I did that it would be easy just to add a link once every 365 days (+1 on leap years) so I might never need that option! ;-)

  4. Kafkaesqui

    Posted 8 years ago #

    Waking up early on New Year's, eh? :D

  5. laurenf
    Member
    Posted 8 years ago #

    Wow, Kafka! Thank you so much! I really appreciate it -- that is exactly what I was looking to do.

  6. laurenf
    Member
    Posted 8 years ago #

    Ooh -- one more question/favor to ask. Right now, the links to the years are on one line. How can I get them to each be on their own line?

    ie:
    2004
    2005
    2006

    instead of: 2004 2005 2006

  7. LoneBoat
    Member
    Posted 8 years ago #

    "How can I get them to each be on their own line?"

    Instead of using:

    <ul>
    <li>Archives</li>
    <li><ul>
    <?php
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date");
    foreach($years as $year) : ?>
    <li><a href="<?php echo get_year_link($year); ?> "><?php echo $year; ?></a></li>
    <?php endforeach; ?>
    </ul></li>
    </ul>

    use:

    <ul>
    <li>Archives</li>
    <li>
    <?php
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date");
    foreach($years as $year) : ?>
    <a href="<?php echo get_year_link($year); ?> "><?php echo $year; ?></a>
    <?php endforeach; ?>
    </li>
    </ul>

    ::EDIT:: Sorry, I misread and thought you were asking the opposite. They should be already, unless your stylesheets are messing with your default <ul>'s.

  8. Kafkaesqui

    Posted 8 years ago #

    LoneBoat is correct; just change the HTML content of the code to represent the way you want to display them. That or, play around a bit with the css for the <li> tags, giving them a property of display:inline;

  9. laurenf
    Member
    Posted 8 years ago #

    Okay, I'm running into some problems. Pasting the code LoneBoat provided didn't work. I tried adding display: inline; first to the li section in my stylesheet, and then with a <li style="display: inline;">. But none of these methods put my years on different lines.

    I think part of the problem might be that the section has an <id="archives"> preceding it, but there is no #archives in my stylesheet that I can change. So you'd think that the selector would do nothing, but when I delete it, the formatting of the "Archives" header changes. Bizarre, no? I don't know why this is proving to be so difficult -- I thought the hard part was behind me.

    Any idea you can think of? I'm so grateful for your help thus far!!

  10. rinaldi
    Member
    Posted 8 years ago #

    This is close to what I'm looking for, but I'd love to find a good way to have it be something you could expand/collapse. I avoid javascript, so I could look at doing this with post/passed options. Would anyone have any idea if this exists or know how to do it effectively?

  11. Dgold
    Member
    Posted 8 years ago #

    Thanks for the code, Kafka! I wanted this too (goes to show...). I put it in place just-now on my as-yet-unannounced WordPress radio site. FWIW, it was initially in a List and I changed it to display like this 1997-1998-1999-2000-...

    For bonus points: How would I make it reverse like this?
    2005-2004-2003-2002-2001-...

  12. Kafkaesqui

    Posted 8 years ago #

    Dgold, to reverse sort change the $years query to:

    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC");

    rinaldi, I'm not sure how you would do a collapsible menu without something like javascript unless you're ok with the page reloading due to each change to the menu. For something where the months appear under a year when selecting it, you'd need a different bit of code than above which queries for years as well months within the current year.

    laurenf, did you try adding an #archives declaration in your stylesheet?

  13. damino
    Member
    Posted 8 years ago #

    Hi it's the code is not working : I see the year in my sidebar but when I click on it it looks for ?m=2005 (with 2005 for example) as it should be ?y=2005

    I don't know a lot about php and nothing about SQL requie so what I have to change ?

    Thanks.

  14. damino
    Member
    Posted 8 years ago #

    Any idea ?

  15. damino
    Member
    Posted 8 years ago #

    Sorry the code is working perfectly well. I just have a "conflict" with the futureposts plugin : I have the Posts AND the Pages in my archives with futureposts active ...

    Thanks.

  16. Aylwin
    Member
    Posted 8 years ago #

    This is just what I was looking for! But, is there any way to exclude future posts? Meaning, if I click on this year's archive, I don't want to see any posts with the date later than today. Is this possible?

  17. Robert
    Member
    Posted 8 years ago #

    Thanks a lot! That's what I was looking for.
    Maybe the next version supports get_archives with yearly?!

    It would be perfect, to show the archives monthly for the actual year and yearly for previous years.
    I guess this code would be a little too complex.

  18. Kafkaesqui

    Posted 8 years ago #

    Aylwin (or anyone else looking to filter out future posts from the archive list), just alter the query code to this:

    $now = current_time('mysql');
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts AND post_date <= '$now' ORDER BY post_date");

    hartlrobert, you may be interested in this plugin:

    http://www.blindmuse.com/blog/2004/03/31/group-archives-by-year/

    (Just happened to stumble across the thread, hence the late reply...)

  19. Robert
    Member
    Posted 8 years ago #

    Kafkaesqui, thanks.

  20. Aylwin
    Member
    Posted 8 years ago #

    Kafkaesqui, thanks for the code. I managed to get it done somehow but your code looks cleaner.

  21. Ninis
    Member
    Posted 8 years ago #

    rinaldi, I'm not sure how you would do a collapsible menu without something like javascript unless you're ok with the page reloading due to each change to the menu. For something where the months appear under a year when selecting it, you'd need a different bit of code than above which queries for years as well months within the current year.

    How exactly do I do this? For the months too appear under a year when I select it? I haven't found any Plugins for this yet.

    Now I have the code written in this thread by Kafka and my Archives look like this:

    2006
    2005
    2004

    Now I would like it to look like this, selecting 2005

    2006
    2005
    January
    February
    ...
    December
    2004

    How do I do this?
    I'm new at PHP-coding but I do know HTML and CSS.

    Thanx

  22. Kafkaesqui

    Posted 8 years ago #

    Ninis, for that we make use of a bit of the behind-the-scenes info WordPress provides on archive (date-based) queries. As the code by now is a bit long for the forums, here's a link to it:

    http://guff.szub.net/source/sample-39204.php

    Let me explain what you'll see there (that is, what I didn't add comments to):

    The first bit of code scoping the WordPress variables $month and $year to global is why I altered my year query vars to $arc_years/$arc_year.

    The second query in the code ($arc_months) collects the months for the current archive year, and goes through a similar foreach loop as for the years, using WP's get_month_link() to display the monthly archive link.

    Note that to display a textual month value (July) rather than a numeric one (7), I do this:

    <?php echo $month[zeroise($arc_month, 2)]; ?>

    This makes use of the global $month array to match number to name. The zeroise() function assures a two digit number (i.e. 01 -- required for a match in the $month array).

  23. gamerscircle
    Member
    Posted 7 years ago #

    Can someone look at http://chris.pirillo.com/ and tell me what his 'archive' code would look like?

    * 2006
    o October (20)
    o September (56)
    o August (83)
    o July (71)
    o June (105)
    o May (143)
    o April (66)
    o March (92)
    o February (118)
    o January (48)
    * 2005
    * 2004
    * 2003
    * 2002

  24. Kafkaesqui

    Posted 7 years ago #

    "Can someone look at http://chris.pirillo.com/ and tell me what his 'archive' code would look like?"

    It's a plugin. In fact, it's this one:

    http://blindmuse.com/blog/2004/03/31/group-archives-by-year/

    blindmuse.com has been down for a while, so I've posted the source for the plugin here:

    http://wordpress.pastebin.ca/209160

  25. ganzua
    Member
    Posted 7 years ago #

    Id it possible to show the year and then the postbypost of that year?

    2006

    * post 12 title
    * post 11 title
    * post 10 title
    * post 9 title
    * post 8 title
    * post 7 title
    ...

    2005

    * post 12 title
    * post 11 title
    * post 10 title
    * post 9 title
    * post 8 title
    * post 7 title
    ...

  26. ganzua
    Member
    Posted 7 years ago #

    up!

  27. Co
    Member
    Posted 7 years ago #

    Thank you very much for the trick !
    I'm now displaying archives by year, and reversed ;)

  28. mikedini
    Member
    Posted 7 years ago #

    Hello:
    I'm using the Blindmuse plugin for my archives, but now the page won't validate. I've used the plugin from here: http://wordpress.pastebin.ca/209160

    The Code:
    /*
    <h2>Archives</h2>

      <?php cg_get_archives_by_year('24', '12', '

    • ', '
    • ', '

      ', '

    ', '

      ', '

    ', false); ?>

    */

    shoot, I can't get the lis and uls to show up.
    It's located here: http://www.pistolotto.com

  29. vkaryl
    Member
    Posted 7 years ago #

    You need to surround code with "backticks" - the weird symbol underneath the tilde (the other weird symbol) on the key to the left of the numeral 1 key on US keyboards....

    Oh, and this plugin from Viper will do something similar: http://www.viper007bond.com/wordpress-plugins/clean-archives-reloaded/

  30. mikedini
    Member
    Posted 7 years ago #

    Thanks...
    Here's the code:

    <h2>Archives</h2>
    <ul>
    <?php cg_get_archives_by_year('24', '12', '<li>', '</li>', '<ul>', '</ul>', '<ul>', '</ul>', false); ?>
    </ul>

Topic Closed

This topic has been closed to new replies.

About this Topic