• WPChina

    (@wordpresschina)


    I am about 98% completed on getting code for my functions.php in WordPress that will generate a list of all the posts on my blog from the last 7 days. However the part where I create code to generate “the last 7 days” of posts is creating problems.

    Here is the code so far, but it lacks all the nonsense I had before that didn’t work to grab only the last 7 days of posts. Can anyone see where I should add the information about “the last 7 days” and how that should be placed?

    function show_post_by_category_and_by_last_week($args = '') {
    	global $post;
    	$categories = get_categories($args);
    	echo "<ul>";
    	$i = 0;
    	foreach ($categories as $category) : $i++;
    	?>
    	<li>
    	      <h2><a href="<?php echo get_category_link($category->term_id);?>"><?php echo $category->name; ?></a></h2>
    	      <ul>
    	        <?php
    	$posts = get_posts('category='. $category->term_id);
    	foreach($posts as $post) :
    	?>
    		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    	<?php endforeach; ?>
    	    </ul>
    	     <div class="read-more"><a href="<?php echo get_category_link($category->term_id);?>">MORE &raquo;&raquo;</a></div>
    	</li>
    <?php
    	if ($i % 3 == 0) {
    		echo "</ul><ul>";
    	}
    	endforeach;
    	echo "</ul>";
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Could put something like this in your foreach loop:

    Test for posts that are so may days old date (in this case 46 days)

    <?php
    $mylimit=46 * 86400; //days * seconds per day
    $post_age = date('U') - get_post_time('U');
    if ($post_age < $mylimit) {
    echo 'this post is within my date limit ';
    }
    ?>

    Thread Starter WPChina

    (@wordpresschina)

    Great, thanks. I scrapped my code and have been playing around with your code for a few days. Your code seems better but it also has its limitations. Below is what I’m working with as code within a page, but it still is having these errors:

    1) It is only outputting the 5 most recent articles, and not the recent articles over the last 7 days as I wish to do.

    2) Though I don’t have it in the code represented below, I’ve also been trying to figure out how to group the posts by category. I scrapped all my previous code because it wouldn’t work. Arrrgggh…

    3) My goal is to have a Page in my blog that outputs posts from the last 7 days, and they are grouped by categories.

    <?php
    /*
    Template Name: OUTPUT PAGE FOR 7 DAYS
    */
    ?>
                <?php
     $posts = get_posts('');
     foreach($posts as $post) :
    $mylimit=7 * 86400; //days * seconds per day
    $post_age = date('U') - get_post_time('U');
    if ($post_age < $mylimit) {
    echo 'this post is within my date limit ';
    }
    
     ?>
                &nbsp; <a href="<?php the_permalink(); ?>">
                  <?php the_title(); ?><br />
                  </a>
                <?php endforeach; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Need part of code to only display last 7 days of posts’ is closed to new replies.