0

I need to list subcategories and the posts that in it, for example:

<h3><a href="#">subcategory</a></h3>
   <ul>
      <li><a href="#">post1</a></li>
      <li><a href="#">post2</a></li>
      <li><a href="#">post3</a></li>
   </ul>

How can I make it work so the permalink of the subcategory will lead to the subcategory and the permalink of the posts will lead to the posts?

0

use the following code should get you through your problem.

just pass the id of parent category in $term_id variable

<?php
    $term_id =  ;    // id for the parent category
    $taxonomy_name = 'category';
    $termchildren = get_term_children( $term_id, $taxonomy_name );    
    foreach ( $termchildren as $child ) :
      $term = get_term_by( 'id', $child, $taxonomy_name );
?> 
<h3><a href="<?php echo get_term_link; ?>"><?php echo $term->name; ?></a></h3>
   <ul>
   <?php  
        $args= array('cat'=>$child,
                    'posts_per_page'=>6                                                 
                    );
        $loop= new WP_query($args);                                               
        if( $loop->have_posts()):while($loop->have_posts()):$loop->the_post(); 
   ?>
          <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>


   <?php 
        endwhile;
        endif;

   ?>

   </ul>
<?php 
    endforeach;
?>   
  • I get an error: Parse error: syntax error, unexpected '$the_query' (T_VARIABLE) – Max B Jun 15 '15 at 7:22
  • Sorry for the delayed reply , updated the code above ,please use it and let me know if you still face any problemO – terminator Jun 17 '15 at 11:19
  • Problem was i miss types $loop as $the_query in $loop= new WP_query($args); & forgot to put ; after $args – terminator Jun 17 '15 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.