Forums

All other posts for the month the post was written (7 posts)

  1. honewatson
    Member
    Posted 2 years ago #

    Is there a query that can show all the other posts written that month.

    So I wrote a post on the 4th of December 2006.

    On the post page I'd like to be able to display all other posts written in December 2006.

    If written on the 6th of August 2006 on the post page it would also display all other posts written in August 2006.

  2. moshu
    Member
    Posted 2 years ago #

    I don't think there is such a query in WP... unless you build it.

  3. honewatson
    Member
    Posted 2 years ago #

    Know ye a way of building such a query?

  4. Sivar
    Member
    Posted 2 years ago #

    While in the Loop, you can assign month and year of your post to variables:

    <?php $month = the_time('m');
    $year = the_time('Y'); ?>

    AFTER that, use query_posts() to retrieve other posts from the displayed post's month/year.

  5. honewatson
    Member
    Posted 2 years ago #

    Thanks sivar I'll give this a whirl.

  6. honewatson
    Member
    Posted 2 years ago #

    These variables do not seem to assign for some reason.

    <?php $month = the_time('m');
    $year = the_time('Y'); ?>
    
    <?php echo $month; ?>

    This displays nothing.

  7. honewatson
    Member
    Posted 2 years ago #

    Ok you've got to use 'get_the_time'.

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    /* get the month and year */
    
    <?php $thispostsmonth = get_the_time('m'); ?>
    <?php $thispostsyear = get_the_time('Y'); ?>
    
    /* get first category id */
    
    <?php
    $cat = get_the_category(); $cat = $cat[0]; $firstcat = $cat->cat_ID; $secondcat = $cat->cat_name;
    ?>
    
    /* show the post */
    
    <h1><a href="<?php the_permalink(); ?>" rel="tag"><?php the_title(); ?></a></h1>
    <?php the_content(); ?>
    <?php endwhile; ?>
    <?php endif; ?>
    
    /* show the post */
    
    <h3><?php echo $secondcat; ?></h3>
    
    <ul>
    
    <?php query_posts("cat=$firstcat"); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <li><a style="color:#cc0066;" href="<?php the_permalink(); ?>" rel="tag"><?php the_title(); ?> </a></li>
    <?php endwhile; ?>
    <?php endif; ?>
    </ul>
    
    /* ############# Display The Posts From The Same Month And Year ############# */
    
    <ul>
    <?php query_posts("year=$thispostsyear&monthnum=$thispostsmonth&showposts=-1&order=DESC"); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <li><a style="color:green;"href="<?php the_permalink(); ?>" rel="tag"><?php the_title(); ?> </a> <span><em><?php the_time('F j, Y'); ?></em></span></li>
    <?php endwhile; ?>
    <?php endif; ?>
    </ul>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags