how show posts in category?

How can I show posts for a single category?

  • 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?

  • Answer:

    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 http://codex.wordpress.org/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: category-{slug}.php - If the category's slug were news, WordPress would look for category-news.php category-{id}.php - If the category's ID were 6, WordPress would look for category-6.php category.php archive.php 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.

Eric at WordPress Visit the source

Was this solution helpful to you?

Other answers

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(); ?>

Andres Yanez

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.

ciprian

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.