• Resolved 1251-1

    (@1251-1)


    I would like to display my posts by the most recent post’s month, on my home page.

    For example, if I published 20 posts this current month, they would be displayed. But as soon as I published a new post in a new month, I would start a fresh page.

    I hope this makes sense.

    Here is my loop at the moment:

    <?
    	if(have_posts())
    	{
    		while(have_posts())
    		{
    			the_post();
    ?>
    			<div>
    				<h1><a href="<?the_permalink();?>" title="<?the_title_attribute();?>"><?the_title();?></a></h1>
    				<?the_content();?>
    			</div>
    <?
    		}
    	}
    	else
    	{
    ?>
    		<div>
    			<h1>Not found</h1>
    			<p>Sorry, but there is nothing here.</p>
    		</div>
    <?
    		get_search_form();
    	}
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Just before your if (have_posts())

    $today = getdate();
    query_posts($query_string . '&year=' . $today["year"] . '&monthnum='.$today["mon"]);

    Related:
    Time Parameters of query_posts

    Thread Starter 1251-1

    (@1251-1)

    I don’t think that code quite works for what I want.

    I don’t want the current month (June), I want the most recent month from when my last published post.

    If my last post was from March, I want all of the March posts on the home page. Now it’s June, if I make a new post all posts from March disappear and the page is made fresh for more June posts.

    <?php
    //get latest post, then get all posts in that one post's year and month
    $args=array(
     'showposts'=>1,
     'caller_get_posts'=>1
      );
    $lastpost = get_posts($args);
    if ( $lastpost ) {
    foreach($lastpost as $single) {
    $fpyear = mysql2date("Y", $single->post_date);
    $fpmonth = mysql2date("m", $single->post_date);
    }
    }
    query_posts($query_string . '&year=' . $fpyear . '&monthnum='.$fpmonth);
    ?>
    Thread Starter 1251-1

    (@1251-1)

    Thank you!

    Hello Michael H, can you help? I have just tried your code above to show ONLY current month’s posts on front page, and it worked successfully. However, when I go to the previous months in the archive section on the right, everything has disappeared! How do I keep only this month’s post on the front page, but keep all the other months’ posts archived in their relevant month category?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display only posts in latest post’s year and month’ is closed to new replies.