0

The website i'm working on has a homepage with posts and at the end of the page a button with view more posts.

When I click that button it sends me to 'all posts'.

On this page I have a problem, the date for the post is wrong.

If you visit: http://www.solarboatleeuwarden.nl/test/berichten/ You can see that every date is set to 30 november. This is either for 2013 and for 2014.

What could be the problem? it's not a problem with my database because the dates on the frontpage are right.

The code for the homepage looks like this:

<div id="news_content">
    <div id="header_line_news">
        <h2>Nieuws</h2>
    </div>
    <div id="news_content_inner">
<?php query_posts('post_type=' . $post_types . '&posts_per_page=3&orderby=date'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post();?>
    <div class="nieuws_text">
    <?php echo do_shortcode( '[hupso]' );?>
    <div class="date_post">
        <?php the_date(); ?>
    </div>
    <h2><?php the_title(); ?></h2>
    <div class="text_holder">
        <div class="img_center_index">
            <?php the_post_thumbnail();?>
        </div>
        <p><?php the_excerpt(); ?></p>
    </div>
        <hr>
    </div>
        <?php endwhile; endif; wp_reset_postdata(); ?>
        <div id="meer_berichten">
            <input type="button" class="meer_berichten" onClick="parent.location='<?php echo $link."berichten";?>'" value="Meer berichten">
        </div>
    </div>
</div>

The code for the 'all posts' page looks like this:

<div id="content">
    <div id="header_line_news">
        <h2>Alle berichten</h2>
    </div>
    <div id="content_overons">
        <?php 
        $years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY year DESC" );
        foreach ( $years as $year ) {
            $posts_this_year = $wpdb->get_results( "SELECT ID, post_title FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );
            echo '<h2>' . $year->year . '</h2>';
            echo '<ul>';
                foreach ( $posts_this_year as $post ) {
                // Display the title as a hyperlinked list item
                echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a>'.the_time('d F').'</li>';
                }
            echo '</ul>';
        }
        ?>
    </div>
2
  • Code please.... Commented Mar 5, 2014 at 10:56
  • Added my code for both pages. Sorry forgot that. Commented Mar 5, 2014 at 10:59

1 Answer 1

1

It could be because you are not supplying post id in time parameter function.

In place of the_time('d F') try using echo get_the_time('d F'm $post->ID) because it will give you the flexibility to supply post id when you are in a loop.

Reference: get_the_time Codex

EDIT

My bad, I guess $post variables are not being set by your method. I have designed the code as per your need.

<?php
//Retrieving all the Distinct years.
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date");

//Loop for printing yearwise Posts
foreach($years as $year){

    $the_query = new WP_Query(array(
        'post_type' =>  'post', //Replace your Post Type HERE. If retrieving from default posts then simple use *post* or remove element.
        'year'  =>$year
    ));
    echo '<h2>'.$year.'</h2>';
    echo '<ul>';
    while($the_query->have_posts()):
        $the_query->the_post();
        echo '<em>'.get_the_date('d F').'</em>';
        echo '<li><a href="'.get_permalink( $post->ID ).'">'.get_the_title().'</a></li>';       
        endwhile;
    wp_reset_postdata();
    }
    echo '</ul>'
?>

WP_query is the authentic way. to retrieve posts within wordpress DB, because it sets all the global and local variables automatically.

4
  • Tried this but this isn't working at all. solarboatleeuwarden.nl/test/berichten Commented Mar 5, 2014 at 12:42
  • What's the publish date of these posts. Are they showing different dates? You can view them in Dashboard. Commented Mar 5, 2014 at 12:54
  • If you visit the homepage: solarboatleeuwarden.nl/test you see that the dates are different for each post. Also these dates are right. But the ones at the 'all posts' page aren't. They're all set to 30 november. Commented Mar 5, 2014 at 12:56
  • I have made an edit. Try using this code. Commented Mar 5, 2014 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.