• Hello I’d like to display all the posts in a category on a page.

    Currently I’m doing using manual else if statement, but I don’t think this is the best way to do it, (below is my code)

    <?php
    /*
    Template Name: categories
    
    */
    ?>
    
    <?php get_header(); ?>
    
    <?php
    //get all categories then display all posts in each term
    $taxonomy = 'category';
    $param_type = 'category__in';
    
    wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0'); 
    
    if ( is_category( '4' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('4'),
    );
    
    } elseif ( is_category( '5' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('5'),
    );
    
    } elseif ( is_category( '6' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('6'),
    );
    
    } elseif ( is_category( '7' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('7'),
    );
    
    } elseif ( is_category( '8' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('8'),
    );
    
    } elseif ( is_category( '9' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('9'),
    );
    
    } elseif ( is_category( '11' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('11'),
    );
    
    } elseif ( is_category( '12' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('12'),
    );
    
    } elseif ( is_category( '16' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('16'),
    );
    
    } elseif ( is_category( '17' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('17'),
    );
    
    } elseif ( is_category( '18' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('18'),
    );
    
    } elseif ( is_category( '19' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('19'),
    );
    
    } elseif ( is_category( '20' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('20'),
    );
    
    } elseif ( is_category( '21' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('21'),
    );
    
    } elseif ( is_category( '22' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('22'),
    );
    
    } elseif ( is_category( '23' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('23'),
    );
    
    } elseif ( is_category( '24' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('24'),
    );
    
    } elseif ( is_category( '25' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('25'),
    );
    
    } elseif ( is_category( '26' )) {
    
      $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'include'  => array('26'),
    );
    
    } else {
    	echo('dwayne');
    }
    
    $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() ) {  ?>
          <div class="category section">
    	    <h3><?php echo 'Category '.$term->name;?></h3>
    
    <ul>
    	    <?php
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
            <div class="post-excerpt-post">
    
    				<h2><?php echo get_the_title(); ?></h2>
    
    			 	<h3><?php echo get_the_date(); ?> </h3>
    
    			 	<div class="comment-number">
    					<a> "><p><?php comments_number( '0', '1', '% ' ); ?></p></a>
    				</div>
    
    				<a>" rel="bookmark"> <span class="side-bar-text"><?php the_excerpt('<p>Read more...»</p>'); ?></span>  </a> 
    
    			</div>
           <?php
          endwhile;
          ?>
          </ul>
          </div>
     <?php
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    
    <?php get_footer(); ?>

    [Please post code or markup between backticks or use the code button. Or better still – use a pastebin. Your posted code may now have been permanently damaged/corrupted by the forum’s parser.]

Viewing 1 replies (of 1 total)
  • Moderator t-p

    (@t-p)

    Try:

    <?php
    function list_post(){
    
        //or you can use the cat id here, check the codex for WP_Query
        $q = new WP_Query(array('category_name' => '<cat slug here>'));
        if($q->have_posts()):
            while($q->have_posts()):the_post();
                the_title();
            endwhile;
        else:
            echo "No posts found!";
        endif;
        wp_reset_postdata();
    }
    
    add_shortcode('list_post', 'list_post');
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘How to display all posts from all categories in WordPress’ is closed to new replies.