Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /** Start copy/paste */
- /**
- * this function does all the work:
- * asks for a list of categories
- * if that is successful,
- * retrieves the posts associated with
- * each category
- */
- function list_posts_by_cat() {
- $taxonomy = 'category';
- $param_type = 'category__in';
- $term_args = array(
- 'orderby' => 'name',
- 'order' => 'ASC'
- );
- $terms = get_terms( $taxonomy, $term_args );
- $list = '';
- if ( $terms ) {
- foreach( $terms as $term ) {
- $qargs = array(
- "$param_type" => array( $term->term_id ),
- 'post_type' => 'post',
- 'post_status' => 'publish',
- 'posts_per_page' => -1,
- 'caller_get_posts'=> 1
- );
- $new_posts = null;
- $new_posts = new WP_Query( $qargs );
- if( $new_posts->have_posts() ) {
- $list .= "<div class='post-list-by-cat'>" . PHP_EOL;
- $list .= "\t<h4>List of Posts in $taxonomy $term->name</h4>" . PHP_EOL;
- $list .= "\t<ul>" . PHP_EOL;
- while ( $new_posts->have_posts() ) :
- $new_posts->the_post();
- $list .= "\t\t<li><a href='" . get_permalink() . "' rel='bookmark' title='Permanent Link to " . the_title_attribute( 'echo=0' ) . "'>" . get_the_title() . "</a></li>" . PHP_EOL;
- endwhile;
- $list .= "\t</ul>" . PHP_EOL;
- $list .= "</div>" . PHP_EOL;
- }
- }
- return $list;
- }
- wp_reset_query();
- }
- add_shortcode( 'listposts', 'list_posts_by_cat' );
- /** END copy/paste */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement