• friend i have a site which has more than 150 category and sub-category
    and they all have their corresponding posts.
    when i click on any category it show all the post of itself as well as it’s all sub categories post.
    i want to show only single post which is in that main category when i click on that main category.
    plz tell me the solution…

Viewing 3 replies - 1 through 3 (of 3 total)
  • You’ll have to craft a Category Template for each of your main categories and use a simple get_posts loop to get just one post in that category. Something like:

    <?php
    if ( is_category() ) {
      $cat = get_query_var('cat');
      $args=array(
        'showposts' => 1,
        'category__in' => array($cat),
        'caller_get_posts'=>1
      );
      $posts=get_posts($args);
      if ($posts) {
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } //is_category
    ?>

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

    Just a note about categories–by WordPress standards, ANY category that has been designated a Parent category is not meant to be checked in the category hierarchy when writing posts. Meaning, only the latest generation category should be checked. Think of it like this, only the youngest generation gets the action.

    It’s okay to have multiple categories assigned a post, as long as the other categories are not designated as parents.

    Thread Starter wp_dev

    (@wp_dev)

    hey michaelh thanks for suggestion…
    but i still have problem becoz as i tell i have many categery and every category has child category upto 4 level(means they have child of child of child of child category) and i want a general patern to get the only post of level three(child of child of child) category not any post of their child category.

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

The topic ‘show only parent category post’ is closed to new replies.