0

I'm trying to make my own theme and I've created an archive template that works fairly well. The template shows all posts, but the problem is that all posts will show if I go to /2018/11/ (posts were created in different months) even though I've added monthnum and year in my query_posts.

<?php
/**
* Template Name: Blog Posts
*/
?>
<?php query_posts('post_type=post&post_status=publish&posts_per_page=10&monthnum='. get_query_var('month') .'&year='. get_query_var('year')); ?>

<?php if( have_posts() ): ?>

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

    <div id="post-<?php get_the_ID(); ?>" <?php post_class(); ?>>

      <div class="row featurette">
        <div class="col-md-7">
          <h2 class="featurette-heading"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
          <p class="lead"><?php echo get_the_excerpt(); ?></p>
        </div>
        <div class="col-md-5">
          <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(500,500, 'class'  => " img-responsive center-block") ); ?></a>
        </div>
      </div>


      <div class="featurette-divider"></div>

    </div><!-- /#post-<?php get_the_ID(); ?> -->

      <?php endwhile; ?>

  <div class="navigation">
    <span class="newer"><?php previous_posts_link(__('« Newer','example')) ?></span> <span class="older"><?php next_posts_link(__('Older »','example')) ?></span>
  </div><!-- /.navigation -->

<?php else: ?>

  <div id="post-404" class="noposts">

      <p><?php _e('None found.','example'); ?></p>

    </div><!-- /#post-404 -->

<?php endif; wp_reset_query(); ?>
  • 1
    I see you're using query_posts instead of modifying the main query with the pre_get_posts filter, what was the reason for doing so? Note that using query_posts this way will break pagination. Also, your template appears to be implementing date archives, but those already exist in WP, and have their own templates, e.g. date.php month.php etc – Tom J Nowell Nov 13 '18 at 2:18
  • @TomJNowell Good question that made me research more. I ended up using the main query instead. – janlindso Nov 18 '18 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.