Support » Fixing WordPress » Query categories in archive page multiple loops

  • Resolved bradyjfrey

    (@bradyjfrey)


    This is a bit tricky for me – I’ve essentially built a type of logbook, where the home page features an indexed list of posts, all auto categorized by month and year – what I would like is to be able to click on a month archive, and view the same format for that month, and so forth. This is the loop code for the home page:

    <div id="topbar">
    <div><h2>Mock Title</h2>
    <h4><em>no.</em> Incident</h4>
    <ol>
    <?php $current_month = date('m'); ?>
    <?php $current_year = date('Y'); ?>

    <?php query_posts("year=$current_year&monthnum=$current_month&cat=2"); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <li><h3>&nbsp;&nbsp;<?php the_title(); ?></h3>
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </li><?php endwhile; ?>
    </ol>
    </div>
    <div><h2>Mock Title</h2>
    <h4><em>no.</em> Incident</h4>
    <ol>
    <?php $temp_query = $wp_query; query_posts("year=$current_year&monthnum=$current_month&cat=3"); ?><?php while (have_posts()) { the_post(); ?>
    <li><h3>&nbsp;&nbsp;<?php the_title(); ?></h3>
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </li><?php } $wp_query = $temp_query; ?>
    </ol>
    </div>
    </div>

    <div id="midbar">
    <div><h2>Mock Title</h2>
    <h4><em>no.</em> Incident</h4>
    <ol>
    <?php $temp_query = $wp_query; query_posts("year=$current_year&monthnum=$current_month&cat=4"); ?><?php while (have_posts()) { the_post(); ?>
    <li><h3>&nbsp;&nbsp;<?php the_title(); ?></h3>
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </li><?php } $wp_query = $temp_query; ?>
    </ol>
    </div>
    <div><h2>Mock Title</h2>
    <h4><em>no.</em> Incident</h4>
    <ol>
    <?php $temp_query = $wp_query; query_posts("year=$current_year&monthnum=$current_month&cat=5"); ?><?php while (have_posts()) { the_post(); ?>
    <li><h3>&nbsp;&nbsp;<?php the_title(); ?></h3>
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </li><?php } $wp_query = $temp_query; ?>
    </ol>
    </div>
    </div>

    That duplicate code does not work for an archive page – it just displays every post in it’s respective category for every month. How would I adjust those loops to be controlled by the archive month/year rather than grabbing the current month/year? Thanks everyone:)

  • The topic ‘Query categories in archive page multiple loops’ is closed to new replies.