0

I am trying to show all posts in each category. I've been searching around and it seems I need to have either the category slug or term_id to do so, like the code below.

<?php query_posts('category_name=MyCategory&showposts=9999'); ?>

I'm currently editing the archive.php how to get the slug name?

Thanks.

1
  • thanks Michael, I was confuse that how to display category wise post on archive.php by query_post, because I am using Magic_fields plugin. really this script help me. Again thaks.
    – user15065
    Apr 11, 2012 at 6:24

2 Answers 2

0

to get the category slug of the category archive:

$cat_slug = get_category(get_query_var('cat'))->slug;

alternatively, to get the category ID of the category archive:

$cat_id = get_query_var('cat');
4
  • Thanks a lot! I really appreciate you help! Just a question: do you know a better way to show all posts than <?php query_posts('category_name=MyCategory&showposts=9999'); ?> Thanks!
    – Muazam
    Aug 27, 2011 at 15:57
  • 1
    @Muazam showposts parameter is deprecated since 2.1, use posts_per_page with arg -1, also it would be better if you use array for query_posts args: <?php query_posts( array( 'category_name' => 'my_category_slug', 'posts_per_page' => -1 ) ); ?>
    – Mamaduka
    Aug 27, 2011 at 16:08
  • @Mamaduka One more question, do you know how to order the result by ID and DESC?
    – Muazam
    Aug 27, 2011 at 20:03
  • 1
    @Muazam add this two parameters: <?php query_posts( array( 'category_name' => 'my_category_slug', 'orderby' => 'ID', 'order' => 'DESC', 'posts_per_page' => -1 ) ); ?>
    – Mamaduka
    Aug 27, 2011 at 20:17
-1

get all the categories from the database

$cats = get_categories();

// loop through the categries

foreach ($cats as $cat) {

setup the cateogory ID

$cat_id= $cat->term_id;

Make a header for the cateogry

echo "<h2>".$cat->name."</h2>";


query_posts("cat=$cat_id&post_per_page=100");  
if (have_posts()) : while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
    <?php endwhile; endif; ?>

done our wordpress loop. Will start again for each category

<?php } ?>

done the foreach statement

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.