SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    WordPress - help with listing posts by year

    Hi

    I've got the following within a WordPress template to provide a list posts by year descending.

    HTML Code:
    <ul>
    <?php
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY post_date DESC");
    foreach($years as $year) : ?>
    <li><a href="<?php echo get_year_link($year); ?> ">News &ndash; <?php echo $year; ?></a></li>
    <?php endforeach; ?>
    </ul>
    It works well except that it's adding another list item onto the bottom of the list of years like this:

    News - 2006
    News - 2005
    News - 2004
    News - 0

    Where the link for "0" brings up posts for 2006 - going to the same page as the 2006 link.

    Can anyone help me remove/exclude that "0" link?

    Thanks in advance.
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose the following will do it:
    PHP Code:
    $years $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_date != 0 ORDER BY post_date DESC"); 
    Not sure why you're getting 0 in the first place. May be something to do with wordpress, or maybe you have some posts, which doesn't have year specified for some reason?

  3. #3
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kyberfabrikken - thank you very much for your help - that did the trick perfectly!

    Yes, I thought that I must have entered something incorrectly too - but I've double checked and I've definitely got it as it should be. The posts appearing in that section are this year's posts so it might be something to do with the way that WP organises these things.

    Oh well - problem sorted for the moment. Thanks again!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •