• Is there an easy way to make the archive.php only show a certain category? I currently have:

    <?php if ( have_posts() ) : ?>
    	<?php $my_query = new WP_Query('category_name=blog');
    	while ($my_query->have_posts()) : $my_query->the_post(); ?>
    		<?php get_template_part( 'content', get_post_format() ); ?>
    	<?php endwhile; ?>
    <?php else : ?>
    Content?
    <?php endif; ?>

    but it shows all posts from the blog category regardless of what part of the archive I’m on. So if I go to /2009/2/, I still see posts from 2010 and on.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Croebh

    (@croebh)

    Bump

    <?php $my_query = new WP_Query(‘category_name=blog’);

    is your query – maybe you need to remove the category_name=blog

    Thread Starter Croebh

    (@croebh)

    But then I get all posts, including those of other categories. I need the archive.php to only look through the archives of one particular category.

    Ok, I didn’t read that right. Give something like this a shot:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    That is how one of my archive.php files is set up. This line of code is the key:

    <?php query_posts('category_name=blog&year=YEAR&monthnum=MONTH'); ?>

    In each of the is_day, is_month, is_year, areas on the top part, let it get_the_date(), but add in another get_the_date( F )[month] and get_the_date( Y )[year] and save it as some variables to plug into that query in the YEAR and MONTH spots. Like this:

    <?php printf( __( '<h1>Yearly Archives: %s', 'twentyeleven' ),  get_the_date( 'Y' ) . '</h1>' );
    $year = get_the_date('Y');
    $month = get_the_date('F');
    ?>

    Out of curiosity: why would you want to restrict archive.php to display only a single category? That’s what category.php is for, and is displayed by www.example.com/category/$category.

    Thread Starter Croebh

    (@croebh)

    https://www.example.com/category/blog/2012/1
    doesnt work, so I was trying to get it to fit into archive.php, so it would be:
    https://www.example.com/2012/1

    Rdlucas2, I got that to work for year, but seemed to break when doing months D:

    Edit: Fixed it by changing
    $month = get_the_date('F');
    to
    $month = get_the_date('m');

    Please mark this as resolved!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Archive.php single category’ is closed to new replies.