2

I have found a tutorial online for creating a custom categories template for my archived posts within a particular category. This however is targetting a specific category ID (see below), is there anyway of creating a template that targets all rather than one specific ID?

<?php
/*
Template Name: Category-Tutorials
 * The template for displaying Archive pages.
 *
 * Used to display archive-type pages if nothing more specific matches a query.
 * For example, puts together date-based pages if no date.php file exists.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */

get_header(); ?>

    <section id="primary">
        <div id="content" role="main">

<header class="page-header">
                <h1 class="page-title">
                    <?php if ( is_day() ) : ?>
                        <?php printf( __( 'Daily Archives: %s', 'twentyeleven' ), '<span>' . get_the_date() . '</span>' ); ?>
                    <?php elseif ( is_month() ) : ?>
                        <?php printf( __( 'Monthly Archives: %s', 'twentyeleven' ), '<span>' . get_the_date( 'F Y' ) . '</span>' ); ?>
                    <?php elseif ( is_year() ) : ?>
                        <?php printf( __( 'Yearly Archives: %s', 'twentyeleven' ), '<span>' . get_the_date( 'Y' ) . '</span>' ); ?>
                    <?php else : ?>
                        <?php _e( 'Tutorials', 'twentyeleven' ); ?>
                    <?php endif; ?>
                </h1>
            </header>

    <ul class="category-7">
        <?php $archive_query = new WP_Query('cat=7&showposts=1000');
        while ($archive_query->have_posts()) : $archive_query->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>
    </ul>

        </div><!-- #content -->
    </section><!-- #primary -->

I was thinking maybe the following could be changed:

<?php $archive_query = new WP_Query('cat=7&showposts=1000');
6
  • If you find the answer to your own question, that's great! Please post your solution as an answer, rather than editing it into the question itself. Commented Dec 19, 2012 at 17:26
  • Hi Chip, i did post this i believe as an answer.... 0_o... sorry for the confusion there :) Commented Dec 19, 2012 at 17:42
  • Kirsty: I only see one answer: the one posted by @userabuser. Commented Dec 19, 2012 at 17:53
  • Hi Chip, not sure what happened then, as i know that i saw an edit. I must have posted it in the wrong box. Apologies Commented Dec 19, 2012 at 18:20
  • I do see an edit to your question, that includes your solution. You just need to move that solution from your question, into an answer. :) Commented Dec 19, 2012 at 18:49

2 Answers 2

3

Remove the cat parameter from your query and name your template file category.php

From the WordPress Codex on Template Hierarchy;

 1. category-{slug}.php - If the category's slug were news, WordPress would look for category-news.php
 2. category-{id}.php - If the category's ID were 6, WordPress would look for category-6.php
 3. category.php
 4. archive.php
 5. index.php

The above denotes the order in which WordPress searches for template files and if it finds one that exists in that hierarchy, it will be used as the template for said condition.

So in this case you want to use category.php to create a global category template.

Of course, you can get creative and do all manner of things to meet all manner of conditions but if your basic requirement is as you describe then the above will suffice.

Edit

Replace this:

<ul class="category-7">
    <?php $archive_query = new WP_Query('cat=7&showposts=1000');
    while ($archive_query->have_posts()) : $archive_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
</ul>

...with this:

<ul class="category-7">
    <?php while ( have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
</ul>
1
  • Actually, get rid of the custom query code entirely. Just name the template file category.php, and WordPress will use it for all category archive index pages. Commented Dec 19, 2012 at 17:25
1

I thought i would contribute my answer to the above. I have coded this to display my categories with a date, the person who posted it and a time it was posted, with a main title to display the title of the category in question.

Please see code and screenshot of what this produces, i hope this proves useful :)

<?php get_header(); ?>
<div id="maincontentwrap" role="main">
<h1 class="page-title"><?php
printf( __( 'Category Archives: %s', 'D-Theme' ), '<span>' . single_cat_title( '', false ) . '</span>' );
                ?></h1>
<div class="pagedivider"></div>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
Written by <?php the_author_posts_link() ?> in <?php the_category(); ?> at <?php the_time('H:h a'); ?> 
<div class="pagedivider"></div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<?php get_footer(); ?>

Unfortunately i cannot screenshot due to lack of reputation on here, however i will show you my site so that you can see this in action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.