Show all posts in category
-
05-05-2021 - |
Domanda
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.
Soluzione
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');
Altri suggerimenti
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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange