AkaMaDdiSk
Member
Posted 3 years ago #
Hi all, I need a way to display the categories and posts in this way on the sidebar, does any one know how I can accomplish this preferably without using a plugin? Basically listing out all of the categories and it's posts in a bulleted list. Thanks much in advance.
- Cat1Post1
- Cat1Post2
- Cat1Post3
Category 2
- Cat2Post1
- Cat2Post2
- Cat2Post3
Category 3
- Cat3Post1
- Cat3Post2
- Cat3Post3
AkaMaDdiSk
Member
Posted 3 years ago #
The category 1 above didn't display corrected on here, but my example category 2 and 3 is how I would like it to be.
<?php
//get all terms (e.g. categories or post tags), then display all posts in each term
$taxonomy = 'category';// e.g. post_tag, category
$param_type = 'category__in'; // e.g. tag__in, category__in
$term_args=array(
'orderby' => 'name',
'order' => 'ASC',
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts in '.$taxonomy .' '.$term->name;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
AkaMaDdiSk
Member
Posted 3 years ago #
Thanks Michael! How can I exclude specific categories from this? Thanks again in advance.
something like this:
<ul class="cat_list">
<?php $cats = get_categories('orderby=count&order=DESC');
// get all categories;
foreach ($cats as $cat) :
echo '<li class="cat_list_item">';
echo '<h2>'.$cat->name.'</h2>';
$args = array(
'posts_per_page' => -1, // max number of post per category
'cat' => $cat->term_id
);
query_posts($args); // get all posts for category
if (have_posts()) :
echo '<ul class="post_list">'; //start the unordered list of posts
while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile;
echo '</ul>'; //end of unordered post list
endif;
wp_reset_query(); ?>
</li> <!--end of li cat_list_item -->
<?php endforeach; ?>
</ul> <!-- end of unordered cat list
untested
AkaMaDdiSk
Member
Posted 3 years ago #
Thanks for that alchymyth, Michael's solution did exactly what I needed, now I just need the ability to exclude certain categories from it.
Change this
$term_args=array(
'orderby' => 'name',
'order' => 'ASC',
);
to something like this (you supply the correc ids to exclude):
$term_args=array(
'exclude' => '1,2,3',
'orderby' => 'name',
'order' => 'ASC'
);
AkaMaDdiSk
Member
Posted 3 years ago #
That worked perfectly. Thanks so much!!