0

Is it possible to show posts for a single category? I tried browsing to the URL http://mysite.com/category/foo which does show all of the posts with category foo, but the posts lose their formatting in the listing.

I'd like a full paginated listing of posts that keeps formatting. Do I need to write a plug-in for this? Can anyone give me some pointers on how to do so?

3 Answers 3

2

It sounds like you actually may need to do very little to get this working. It seems as though you have a template hierarchy issue. In WordPress, a number of variables are considered when making a decision about which template should be used to show the content generated by a specific URL. If you take a look at WordPress' Template Hierarchy, you can see how these decisions are made.

In the case of taxonomies, and more specifically, WordPress' built-in category taxonomy, WordPress looks for these templates in the following order:

  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

These files are located in your active theme's folder. If the first file is located, it will be used to display your content. If not, WordPress will look for the 2nd file and so on. If nothing is located, it will default to your index.php file.

Now, in your situation, it really sounds like you have an index.php file that is displaying posts in a way that you really like; however, when you try to access a category page, the display is different. This to me suggests that WordPress is displaying one of the files listed 1-4 and not showing the same template as your regular blog posts, which is likely index.php. So, you can fix this by doing one of two things (note, please back up your files before doing either of these options).

1) Figure out which template is displaying your category page. Copy the contents of index.php to this file

2) Delete the file that is displaying your category page and let index.php display the page.

By doing either of these options, you will be avoiding any hacky solutions that break out of the WordPress way of doing things, which will save you tons of headaches. Additionally, pagination will work without any custom solutions.

I know this may not be super simple, but feel free to ask more about this and hopefully we can get you straightened out.

2
  • I don't have enough rep to +1 yet, but this sounds very promising - I will try this out and report back.
    – Eric
    Nov 28, 2011 at 19:07
  • Works beautifully, excellent answer.
    – Eric
    Dec 11, 2011 at 0:44
1

You can use a custom loop if you want to show a list of category posts, something like this:

<?php

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $query_args = array(
               'posts_per_page' => 5,
               'category_name' => 'foo',
               'paged' => $paged
    );

    $the_query = new WP_Query($query_args);

    if ($the_query->have_posts()) : while ($the_query->have_posts()) :  
             $the_query->the_post(); ?>

              <?php // any other template tag ?>

              <?php the_title(); ?>

              <?php the_post_thumbnail(); ?>

               <?php the_excerpt(); ?>

<?php endwhile; endif; ?>

<div class="navigation">
           <div class="alignleft"><?php next_posts_link('&larr; Older Entries') ?></div>
           <div class="alignright"><?php previous_posts_link('Newer Entries &rarr;') ?></div>
</div><!-- /.navigation -->

<?php wp_reset_query(); ?>
1
  • Why use a custom loop in this situation? Wouldn't it work better to adhere to the template hierarchy, which do the loop for you?
    – tollmanz
    Nov 28, 2011 at 1:55
-1

Just specify the category id in a query

$args = array(
'cat'      => 22,
'order'    => 'ASC'
);
query_posts( $args );

Read more here: https://codex.wordpress.org/Function_Reference/query_posts.

1
  • Very risky to use query_posts in such a situation where it is not needed. You may change a lot of query vars, which could lead to many bugs that are very difficult to debug. Take a look at the top answer for this question to see what happens when using query_posts: wordpress.stackexchange.com/questions/1753/…
    – tollmanz
    Nov 28, 2011 at 1:57

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.