Support » Plugins » Archives by YEAR (this is different!)

  • laurenf

    (@laurenf)


    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.

Viewing 15 replies - 1 through 15 (of 32 total)
  • Kafkaesqui

    (@kafkaesqui)

    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.

    Beel

    (@beel)

    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! 😉

    Kafkaesqui

    (@kafkaesqui)

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

    laurenf

    (@laurenf)

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

    laurenf

    (@laurenf)

    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

    LoneBoat

    (@loneboat)

    “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.

    Kafkaesqui

    (@kafkaesqui)

    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;

    laurenf

    (@laurenf)

    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!!

    rinaldi

    (@rinaldi)

    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?

    Dgold

    (@dgold)

    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-…

    Kafkaesqui

    (@kafkaesqui)

    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?

    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.

    Any idea ?

    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.

    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?

Viewing 15 replies - 1 through 15 (of 32 total)
  • The topic ‘Archives by YEAR (this is different!)’ is closed to new replies.