WordPress.org

Ready to get started?Download WordPress

Forums

Show posts from only the current month (5 posts)

  1. benshep
    Member
    Posted 4 years ago #

    Hello,

    I am setting up a wordpress theme that is for a newspaper. What i want to do with the front page is to have only the articles from the current month or bimonthly shown. Does anyone know an easy way to do this. I was thinking of using the monthnum function for wp_query but couldnt figure out how to set the variable for monthnum=variable to the users month.

    Im not sure if this is the best way to tackle this problem if anyone has any ideas let me know and it would be greatly appreciated.

    thanks,
    ben

    my current loop runs for each category and looks like this

    <?php query_posts('category_name=news'); ?>

    <?php while (have_posts()) : the_post(); ?>

    <?php if($post->ID != $featured_ID) { ?>

    <h3>" title="<?php the_title(); ?>"><?php the_title(); ?></h3>

    <small><?php echo get_post_meta($post->ID, 'author', true)?> - <?php echo get_post_meta($post->ID, 'program', true)?></small>
    <?php the_excerpt(); ?>
    <p>">Continue Reading...</p>
    <? } ?>
    <?php endwhile; ?>

  2. xdesi
    Member
    Posted 4 years ago #

    How do you mean by setting the variable to the users month?

    You can get the current month using:
    month = date('m');

    So the current month would then be contained in the variable $month to use in the WP query like:

    <?php query_posts("category_name=news&monthnum=$month"); ?>

  3. esmi
    Forum Moderator
    Posted 4 years ago #

    Not tested this but what about:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $today = getdate();
    $args = array(
    	'category_name' => 'news',
    	'year' => $today['year'],
    	'monthnum' => $today['mon'],
    	'paged' => $paged
    );
    query_posts($args);
    ?>
  4. benshep
    Member
    Posted 4 years ago #

    @esmi

    I tried the code that you have there and it worked for the month. The situation I have is the blog is for a newspaper that is published every 2 weeks, i see your code filters for the current month, do you think there would be an easy way to filter the posts so that only the current issue is present on the front page every two weeks.

    The problem i'm having is that the posts on the front page sometimes are from the previous issue which i do not want displayed.

    basically i want the posts to follow this pattern

    week 1 & 2

    only issue 1 posts on front page

    week 3 & 4

    only issue 2 posts on front page

    *posts for the issues all posted on the first day of the week

    thanks,
    ben

  5. esmi
    Forum Moderator
    Posted 4 years ago #

    There's a couple of time-based examples on the Codex page for query_posts that might get you started.

Topic Closed

This topic has been closed to new replies.

About this Topic