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.

I have a WordPress website that has a category with two subcategories.

Now, how is it possible to show the content (the subcategories items/content) on the parent category?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

One possibility:

<?php 
    $sub_cats = get_categories('parent=' . get_query_var('cat'));
    if( $sub_cats ) :
        foreach( $sub_cats as $sub_cat ) :
            $sub_query = new WP_Query( array(
                'category__in' => array( $sub_cat->term_id ),
                'posts_per_page' => -1)
            );
            if ( $sub_query->have_posts() ) :
                while( $sub_query->have_posts() ) : $sub_query->the_post();
                    //your output//
                endwhile;
            endif;
        endforeach;
    endif;
?>

http://codex.wordpress.org/Function_Reference/get_categories http://codex.wordpress.org/Class_Reference/WP_Query

share|improve this answer
    
Thanks for the code, but it doesn't do the trick. Im not sure if you know what I mean. I would like to have a parent category, with its subcategory titles listed. Under each subcategory title I want the content related to that specific subcategory. I know its complicated :) sorry –  Bagger Feb 2 '12 at 10:19
    
the code is meant to go into category.php; if you need to show the parent category, add the new code after the existing loop. what are you using as //your output// ? what is the output of the suggested code? how you style the output with html tags and css, is your problem. can you paste the full code of your category template into a pastebin.com and post the link to it here? –  Michael Feb 2 '12 at 11:20
1  
In what way does this not work @Bagger? @Michael's answer sets up the query for you, you just need to fill in what you want it to display... –  Stephen Harris Feb 2 '12 at 11:23
    
I dont understand what you mean with output? Do I have to put in <?php or just normal html? –  Bagger Feb 2 '12 at 11:31
    
pastebin.com/cBh7tD5S here is my code –  Bagger Feb 2 '12 at 11:34
show 2 more comments
$cat = get_query_var( 'cat' );
$args = array(
    'post_status' => 'publish',
    'tax_query'   => array(
        array(
            'taxonomy' => 'category', //may be categories, I never remember, try both
            'field'    => 'slug', //may need to use ID, depends on output
            'terms'    => $cat
            //children are included by default
        )
    )
);
$posts = new WP_Query( $args );

That will give you an array of post objects. You can use setup_postdata() to feed that into a loop if you want. This code will be expansible as you add subcategories they will be queried as well.

Docs: WP_Query, setup_postdata()

update

<?php
$cat = get_query_var( 'cat' );
$args = array(
    'post_status' => 'publish',
    'tax_query'   => array(
        array(
            'taxonomy' => 'category', //may be categories, I never remember, try both
            'field'    => 'id', //may need to use slug, depends on output
            'terms'    => $cat
            //children are included by default
        )
    )
);  
$my_posts = new WP_Query( $args );
?>

<?php if ( $my_posts->have_posts()) : while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>     
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <div class="entryCategory">              
            <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_post_thumbnail( 'thumbnail', array( 'class' => 'alignright' ) ); ?></a>
        </div>
share|improve this answer
    
how do I do that setup_postdata() as you mention? –  Bagger Feb 2 '12 at 11:40
    
Can you be a bit more specific about which part of the documentation on it (that I linked in my answer) you do not understand? –  m0r7if3r Feb 2 '12 at 11:41
    
when I add the code from you @ m0r7if3r I get this on my web: graaecopenhagen.com.tmp2.franklyweb.dk/graae/category/belts –  Bagger Feb 2 '12 at 11:42
    
change field to id and see if that fixes anything. That error probably isn't being caused by my code...not directly at least. A bit of context for the code would be more than helpful in diagnosing the issue. –  m0r7if3r Feb 2 '12 at 11:46
    
it doesn't change anything to change field to id. the code is here: pastebin.com/cBh7tD5S –  Bagger Feb 2 '12 at 11:52
show 15 more comments

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.