Support » Fixing WordPress » Show posts belonging to children categories of parent category

  • Resolved taghaboy

    (@taghaboy)


    Hi,
    I use this loop to show all the cats under the cat id:2

    <?php query_posts('cat=2&orderby=rand&showposts=8'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    ...

    and i’d like to do another loop to show only the childe cats of the parent id:2, how can i do it?

    Thanks in advance

Viewing 8 replies - 1 through 8 (of 8 total)
  • use the post_parent argument. See the template tag, query_posts(), for other arguments you can use.

    i dont know how can i do it with this loop :

    <?php query_posts('cat=2&orderby=rand&showposts=8'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    ...

    Oops, post_parent is for pages.

    Try:

    <?php
    //get all posts for children of category $cata
    $cata = 6;
    $taxonomy = 'category';
    $cata_children = get_term_children( $cata, $taxonomy );
    
    $args=array(
      'category__in' => $cata_children,
      '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 belonging to Category A children';
      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().
    ?>

    It work, thank you.

    i’d like to share my code if any one wanna it,

    <ul class="gallery-more">
    	<?php
    	//get all posts for children of category $cata
    	$cata = 3;
    	$taxonomy = 'category';
    	$cata_children = get_term_children( $cata, $taxonomy );
    
    	$args=array(
    	  'category__in' => $cata_children,
    	  'post_status' => 'publish',
    	  'posts_per_page' => -1,
    	  'showposts'=>8,
    	  'orderby'=>rand,
    	  'caller_get_posts'=> 1
    	);
    	$my_query = null;
    	$my_query = new WP_Query($args);
    	if( $my_query->have_posts() ) {
    	  echo 'List of Posts belonging to This Category';
    	  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    				<?php // call the custom field key "exemple_image1" //
    				$var_img01 = get_post_meta($post->ID, "exemple_image1", TRUE);?>	  
    
    				<li>
    					<?php // Capture the custom field "image 1" //
    							if ( $var_img01 ) { ?>
    								<a title="Read more about : <?php the_title(); ?>" href="<?php the_permalink() ?>">
    									<img alt="<?php the_title(); ?>" src="<?php echo $var_img01; ?>"/>
    								</a>
    					<?php } else { ?>
    									<img src="<?php bloginfo('stylesheet_directory'); ?>/img/no-img.jpg" title="No image" width="100" height="100"/>
    					<?php } ?>
    						<div class="bg"/>
    
    				</li>
    	<?php endwhile; }
    		wp_reset_query();  // Restore global post data stomped by the_post().
    	?>
    </ul>

    The idea her, is to create a personal gallery of a products.

    Enjoy.

    Probably better to change

    'posts_per_page' => -1,
    'showposts'=>8,

    to

    'posts_per_page' => 8,

    Thanks again, i do the changes.

    i try to do this but i have no text:

    echo '<p class="titre-button"><span>List of Posts belonging to This Category :</span></p>';

    taghaboy

    (@taghaboy)

    soory, all are ok, my css hide class=”titre-button”.

    Thanks for all

    How can this be modified to Show posts belonging to children categories of the current category of the single post that is currently showing?

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Show posts belonging to children categories of parent category’ is closed to new replies.