Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Example:

video cat id = 4 video cat child category id's = 7,8,9 news cat id = 5 news cat child category id's = 11,12,13

I wanna display choosen categories ( like video and news ) in the different archive.php?

Is it possible thanks.

( sorry about my bad Eng )

Thanks for Chris S but its not what i want to do.

  <?php
$catPosts = new WP_Query();
$catPosts->query( array( 'category__and' => array(5,11,12,13), 'posts_per_page' => 5, 'orderby' => 'date', 'order' => 'DESC' ) );
  while ($catPosts->have_posts()) : $catPosts->the_post(); ?>
  <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
  <div class="meta">
   By <?php the_author() ?>
  </div>
<div class="content">
  <?php the_excerpt(); ?>
</div>

When I wrote the parent category id, i dont wanna write its child category id's. When i choose the parent category id the system should its child category ids in the same archive page.

share|improve this question
    
Do you want your child categories on separate archive pages, or all of them to be in one? –  chris_s Jul 29 '11 at 13:50
    
separate archive pages... example: the news category archive ( included with child categories ) in archive-news.php the videos category archive ( included with child categories ) in examle archive-video.php –  Fatih Toprak Jul 29 '11 at 15:26

3 Answers 3

up vote 1 down vote accepted

Create 2 pages category-news.php & category-video.php & put this code in them. Then customize the markup for both as you like

<?php
$children = get_categories('child_of'=>get_query_var('cat'));
$cat = array(get_query_var('cat'));
foreach($children as $child)
    $cat[] = $child->term_id;
$catPosts = new WP_Query( array( 'category__in' => $cat, 'posts_per_page' => 5, 'orderby' => 'date', 'order' => 'DESC' ) );
  while ($catPosts->have_posts()) : $catPosts->the_post(); ?>
  <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
  <div class="meta">
   By <?php the_author() ?>
  </div>
<div class="content">
  <?php the_excerpt(); ?>
</div>
share|improve this answer
    
Thanks Aggarwal this worked. –  Fatih Toprak Oct 20 '12 at 12:13

Taking a look at the Template Hierarchy in WordPress could be useful. WordPress allows you to use different templates for different categories based on Category slug (category-{slug}.php) or Category ID (category-{id}.php).

As for different post types, you could do archive-{post_type}.php.

Essentially, you could simply copy archive.php and rename it to category-{slug}.php, category-{id}.php or archive-{post_type}.php (depending on what suits you best) and then modify the existing code in the template to your needs. Just a thought.

share|improve this answer

So you already know that you need to create a archive-category.php template for each category. Within each template you would call a custom query for each of those categories and their child categories. For example:

<?php
$catPosts = new WP_Query();
$catPosts->query( array( 'category__in' => array(5), 'posts_per_page' => 5, 'orderby' => 'date', 'order' => 'DESC' ) );
  while ($catPosts->have_posts()) : $catPosts->the_post(); ?>
  <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
  <div class="meta">
   By <?php the_author() ?>
  </div>
<div class="content">
  <?php the_excerpt(); ?>
</div>

This one shows the "news" category with it's children included. Note that you can place any of your own tags to output whatever HTML you want.

share|improve this answer
    
Dude, when i choose a parent category id, the system should display its child category in the same archive page. I dont wanna write all child categories ids.. –  Fatih Toprak Jul 29 '11 at 21:01
    
Sorry about that. I edited it to query "news" category and it's children in your archive. –  chris_s Aug 1 '11 at 15:24
    
did this work for you? –  chris_s Aug 2 '11 at 16:26
    
didnt work chris when i click the sub category the regular archive page came. –  Fatih Toprak Aug 11 '11 at 19:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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