I have a CPT Called "Membres" (members in English). So after that I created a category called "Membres" and his child categories.

Parent category "Membres"
 - Subcategory1 (3 post inside)
 - Subcategory2 (4 post inside)
 - Subcategory3 (1 post inside)

The solution I need is to make a kind of loop (outside the loop), but making an output of archives of the categories I defined before (calling the functions). This will be part of a template for a static page. The scheme I like to obtain is the next:

<h1>Title "Membres"(calls the title of the page, working)</h1>

 - <h2><a>Name of "Subcategory1"</a></h2>
  - <p>Description of "Subcategory1"</p>
   - <posts Subcategory1>

- <h2><a>Name of "Subcategory2"</a></h2>
  - <p>Description of "Subcategory2"</p>
   - <posts Subcategory2>

 - <h2><a>Name of "Subcategory3"</a></h2>
  - <p>Description of "Subcategory3"</p>
    - <posts Subcategory3>

The problem is with the element: <h2><a>Name of "Subcategory#"</a></h2>. I need it to work as a kind of header for each section of the child category (so also works as a link to the category itself). Right now I obtain the "Category name" as much times as the numbers of post are in each category, but no the Category name only one time. I tried to find out a solution here and there but with any result at the moment. Also I like to continue using this method, and no to move the stuff to taxonomies.

The CPT setup in wordpress functions.php:

// Register Custom Post Type
function custom_post_type() {

    $labels = array(
        'name'                => _x( 'Membres', 'Post Type General Name', 'mem' ),
        'singular_name'       => _x( 'Membre', 'Post Type Singular Name', 'mem' ),
        'menu_name'           => __( 'Membres', 'mem' ),
        'parent_item_colon'   => __( 'Membre principal', 'mem' ),
        'all_items'           => __( 'Tots els membres', 'mem' ),
        'view_item'           => __( 'Veure membre', 'mem' ),
        'add_new_item'        => __( 'Afegir membre', 'mem' ),
        'add_new'             => __( 'Nou membre', 'mem' ),
        'edit_item'           => __( 'Editar membre', 'mem' ),
        'update_item'         => __( 'Actualitzar membre', 'mem' ),
        'search_items'        => __( 'Cercar membres', 'mem' ),
        'not_found'           => __( 'No hi ha membres', 'mem' ),
        'not_found_in_trash'  => __( 'No hi ha membres a la paperera', 'mem' ),
    $rewrite =<h2><?php arraysingle_cat_title(
        'slug'                => 'membre',
        'with_front'          => true,
        'pages'               => true,
        'feeds'               => true,
    $args = array(
        'label'               => __( 'membres', 'mem' ),
        'description'         => __( 'Membres', 'mem' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
        'taxonomies'          => array( 'category', 'post_tag' ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'           => get_bloginfo('template_directory').'/img/iconos?></users.png',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'rewrite'             => $rewrite,
        'capability_type'     => 'post',
  <p><?php echo register_post_typecategory_description( 'membres', $args );


// Hook into the 'init' action
add_actionget_category_by_slug( 'init', 'custom_post_type', 0 'category_name_slug');

The portion of code I am using for the archive of one Subcategory:

<?php query_posts(array( 
          'post_type' => 'membres',
          'category_name' => 'investigadors',
          'showposts' => 20,
         ->term_id )); ?>

      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<!-- PART TO SOLVE-->   
<h2><a>Name of "Subcategory3" Wordpress function PHP</a></h2>
<p>Description of "Subcategory3" PHP"category_description"</p>
<!-- PART TO SOLVE-->   

          <?php include (TEMPLATEPATH . '/membresmem.php'); ?>
           <?php endwhile; else: ?>
           <p>Sorry, no posts matched your criteria.</p>
           <?php endif; ?>
          ?php wp_reset_postdata();?>

Thanks to @CYBNET for mention the "clue thing"

Added the correct functions for category title and category description after the query and before the loop:

 <?php query_posts(array( 
          'post_type' => 'posttype',
          'category_name' => 'category',
          'showposts' => 20,
          )); ?>

    <h2><?php single_cat_title(''); ?></h2>
    <p><?php echo category_description( get_category_by_slug('category_name_slug')->term_id ); ?></p>

          <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

1 Answer 1


You are getting the "Category name" as much times as the numbers of post are in each category because you have the code to output the "Category name" inside the loop, so the "Category name" is printed in every loop itineration. You could fix this part, for example in this way:

<?php if ( have_posts() ) { ?>

    <h2><a>Name of "Subcategory"</a></h2>
    <p>Description of "Subcategory"</p>

    //Now you can start the loop
    while (have_posts()) {
        //don't use include, use get_template_part() instead
        //include (TEMPLATEPATH . '/membresgim.php');

<?php } else { ?>

    <p>Sorry, no posts matched your criteria.</p>

<?pph } ?>

You must know also that is recomended to avoid the use of query_posts() function for several reasons. In your case, as you are going to run several secondary loops, I think is better to use a new instance of WP_Query for each loop. A quick example (for what I understand from your question and comments):

//Change by the ID of your top categoy
$root_category = 45;
$subcategories =  get_categories('child_of='.$root_category);  
foreach  ($subcategories as $cat) {
    $args = array(
                category__in => array($cat->cat_ID),
    $the_query = new WP_Query($args);
        <h2><a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo $cat->nicename; ?></a></h2>
    while($the_query->have_posts()) {
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php } ?>
    <?php wp_reset_postdata(); ?>
<?php } ?>
  • Thanks, but maybe I wasn't so clear. But I was using diferent ways to get the name of the category of the posts below. I was using for instance "get_category", "the_category" and so on. Anyway, I will try meantime the WP_query stuff to better use of the "loop".
    – marxslope
    Sep 18, 2013 at 20:34
  • You didn't talk about any method to get the category name, you didn't put any code for this and you didn't talk about you have a problem with the category name except that it was printed several times, so I give answer to your question, at least as I understood it. You have edited the question and I'm still see the same problem: "I get the category name several times". What is your real problem? Please, be so much clear as possible.
    – cybmeta
    Sep 18, 2013 at 21:28
  • Ey… the whole question with his temp solution is updated. Thanks, PD: ¡Gracias por las molestias @cybnet !
    – marxslope
    Sep 18, 2013 at 22:09
  • I missed to put the funtions for the category_title and category description to explain the situation. Now it's clear. Thanks to also to mention the clue thing, which is to put the funtion before the "loop" (I forgot it,damm). The functions which I founded to work are published. I don't know if with query post is the most correct. With a little bit of effort I will change it to WP_Query. I must to take a look to the Codex documentation !! Ey… The temp solution is updated. PD: @cybnet ¡Gracias !
    – marxslope
    Sep 18, 2013 at 22:13
  • I'm still not getting you. In your question you say that your problem is the repeting "Catgoery name" but my answer, which solves the problem, doesn't satisfy you and you say something like that is not the problem. Is it possible that what you want is to get all posts under the same top "Category" and group them by child categories and that your problem is related with how to do it and not with the repeating "Category name"?
    – cybmeta
    Sep 19, 2013 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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