WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show posts belonging to children categories of parent category (9 posts)

  1. taghaboy
    Member
    Posted 4 years ago #

    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

  2. MichaelH
    Member
    Posted 4 years ago #

    use the post_parent argument. See the template tag, query_posts(), for other arguments you can use.

  3. taghaboy
    Member
    Posted 4 years ago #

    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(); ?>
    ...
  4. MichaelH
    Member
    Posted 4 years ago #

    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().
    ?>
  5. taghaboy
    Member
    Posted 4 years ago #

    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.

  6. MichaelH
    Member
    Posted 4 years ago #

    Probably better to change

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

    to

    'posts_per_page' => 8,
  7. taghaboy
    Member
    Posted 4 years ago #

    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>';

  8. taghaboy
    Member
    Posted 4 years ago #

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

    Thanks for all

  9. Pete
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic