• I am trying to display a list of all the posts based on their sub-category. I have it partially working, but for the last “foreach” loop it displays the title (link) with post info (content, tags, categories) twice.

    I have a category hierarchy of this:

    Articles
       |-> Adults
       |-> Students
       |-> Kids

    So, when you go to the “Articles” page there is a list like this:

    Articles
    
    * Adults
         * Post Title 1 for Adults
         * Post Title 2 for Adults
         * Post Title 3 for Adults
    * Students
         * Post Title 1 for Students
         * Post Title 2 for Students
         * Post Title 3 for Students
    * Kids
         * Post Title 1 for Kids
         * Post Title 2 for Kids
         * Post Title 3 for Kids

    This is the code I am using:

    <?php
    	if (is_page( 'articles' )) {
    		$args = array(
    			'orderby' => 'id',
    			'parent' => 9, // Articles
    			'exclude' => 122 // Subcategory Year
    		);
    
    		$cats = get_categories( $args );
    			foreach($cats as $cat) {
    				$args=array(
    					'orderby' => 'date',
     				    'order' => 'ASC',
    					'showposts' => -1,
    					'category__in' => array($cat->term_id),
    					'caller_get_posts'=>1
    				);
    				$posts=query_posts($args);
    				echo '<h2><a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a></h2> ';
    					foreach($posts as $post) {
    					  if(have_posts()) : while(have_posts()) : the_post(); ?>
                          <a href="<?php the_permalink();?>"><?php the_title(); ?></a><br>
                          <?php endwhile; endif; ?>
    					<?php } // foreach($posts
    			} // foreach ($cats
    	} // close if articles
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try this instead:

    <?php
    if ( is_page( 'articles' ) ) :
        $args = array(
            'orderby' => 'id',
            'parent'  => 9, // Articles
            'exclude' => 122 // Subcategory Year
        );
        $cats = get_categories( $args );
    
        foreach( $cats as $cat ) :
            $args = array(
                'orderby'             => 'date',
                'order'               => 'ASC',
                'posts_per_page'      => -1,
                'category__in'        => array( $cat->term_id ),
                'ignore_sticky_posts' => 1
            );
            $query = new WP_Query( $args );
    
            if ( $query->have_posts() ) {
                echo '<h2><a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a></h2> ';
                while( $query->have_posts() ) : $query->the_post();
                    ?>
                    <a href="<?php the_permalink();?>"><?php the_title(); ?></a><br>
                    <?php
                endwhile;
                wp_reset_postdata();
            }
    
        endforeach;
    endif;
    ?>

    I replaced query_posts() with WP_Query(), which is almost always the better method to use.

    Also, I replaced the deprecated caller_get_posts argument with ignore_sticky_posts. From the Codex:

    ignore_sticky_posts (boolean) – ignore sticky posts or not (available with Version 3.1, replaced caller_get_posts parameter). Default value is 0 – don’t ignore sticky posts. Note: ignore/exclude sticky posts being included at the beginning of posts returned, but the sticky post will still be returned in the natural order of that list of posts returned.

    Thread Starter mentalward

    (@mentalward)

    Thank you so much! That’s prefect. I am fairly new to this but I knew it was something as simple a clearing the array’s data.

    Thank you!

    Also, I read that on the reference page, but I figured I’d update it after I got it completely working.

    No problem, glad I could help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Sub-Category List of Posts’ is closed to new replies.