List Parent Posts; Exclude Child Post

Community Forums Forums General Discussion List Parent Posts; Exclude Child Post

This topic is: not resolved

This topic contains 2 replies, has 3 voices, and was last updated by  genwrock 4 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #111260

    makingyouthink
    Member
    Post count: 9

    I have been playing around with this code to generate a list of categories and their post. Our design includes both parent and child categories, however, if we show the posts in the Parent Categories, it lists all the parent and child posts. Anyone have any suggestions on how to filter only the Parent Posts.

    Thanks to Lauren @ OnceCoupled for helping me get this far. See the original code at http://www.studiopress.com/forums/topic/display-all-posts-from-all-categories-on-a-single-page/

    <?php
    /**
    * Template Name: Category Index
    * Created by: Lauren Gray Designs (http://oncecoupled.com)
    * Modified by: Mark Hanlon
    * URL: http://www.makingyouthink.ca
    */
    
    /**
    * VERSIONS
    1.0 Created by Lauren Gray Designs
    1.1 Revisions by Mark Hanlon
    1.5 Revised to include page text and page title
    2.0 Revised HTML formatting to remove Catagory from unordered (bulleted) list; assigned as H3
    2.1 Revised to include Parent Catagory and Sub-Catagory
    */
    
    // Remove the loop and replace it with our own
    // get top level terms
    
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    
    // Include Page title and allow for text befor Coded Cagatories
    add_action( 'genesis_loop', 'genesis_standard_loop', 5 );
    
    // Custom Function to display all Parent, Catagories and related Post
    add_action( 'genesis_loop', 'LGD_add_archive', 9 );
    
    function LGD_add_archive() {
    
      // Include category ID as ID, ID to exclude
      $Cat_Exclude = '116';
      $Post_Exclude ='';
      
      $args = array(
    	'exclude' => $Cat_Exclude,
    	'orderby' => 'name',
    	'parent' => 0,
    	'hide_empty'=> TRUE,
      
      );
      
      $parent = get_categories( $args );
      $category = get_the_category();
      $cat = get_category($category[0]->term_id);
      $parent_catid = $cat->category_parent;
      
      
      // List each parent category
      foreach ( $parent as $category ) {
    	echo '<h1><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br/></h1>';
    
    	  // Arguments for Post in Parent - trouble listing only post in Parent category and not to include child category
    	  $args = array(
    		  'exclude' => $Post_Exclude,
    		  'posts_per_page' => -1, 
    		  'category' => $category->term_id,
    		  'include_children' => false,
    		  'depth' =>1,
    		  );
    	
    	  $myposts = get_posts( $args );
    
    	  // List the posts in said category
    	  foreach ( $myposts as $post ) : setup_postdata( $post );
    	//		echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
    	echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) ;
    	  echo '</a> written by ' ;    
    	  the_author_posts_link();
    	  echo ' on ';
    	  echo get_the_date ('F j, Y', $post);
    	  echo '</li>'; 
    	endforeach; 
    	  
    	  // Close everything up
    	  echo '</ul>';
    	
    	$args = array(
    	'exclude' => $Cat_Exclude,
    	  'type' => 'post',
    	  'orderby' => 'name',
    	  'child_of'=> $category->term_id,
    	  'post_status' => 'publish'
    	  );
    	  
    	$categories = get_categories($args);
    	
    	// List each category
    	foreach($categories as $category) { 
    	
    	  // Output the cateogry (and link to it)
    	  echo '<h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name . '</a></h3><ul>';
    	  
    	  $args = array(
    		  'exclude' => $Post_Exclude,
    		  'posts_per_page' => -1, 
    		  'category' => $category->term_id );
    	
    	  $myposts = get_posts( $args );
    	  
    	  // List the posts in said category
    	  foreach ( $myposts as $post ) : setup_postdata( $post );
    	//		echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
    	echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) ;
    	  echo '</a> written by ' ;    
    	  the_author_posts_link();
    	  echo ' on ';
    	  echo get_the_date ('F j, Y', $post);
    	  echo '</li>'; 
    	endforeach; 
    	  
    	  // Close everything up
    	  echo '</ul>';
    	}
    	}
    }
    
    genesis();
    http://www.test-me.ca/index-catagories-v2/
    #111266

    braddalton
    Participant
    Post count: 10248
    #111268

    genwrock
    Member
    Post count: 678

    place ‘post_parent’ => 0 into $args array. You will use it in appropriate position. Then you will get correct result


Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.