• Resolved aen

    (@aen)


    Ok here’s an imaginary structure of a site. The actual structure is more complex but I’m using an imaginary one for clarity.

    • Category 1
    • Post 1.1
    • Category 1.1
    • Post 1.1.1
    • Post 1.1.2
    • Category 2
    • Post 2.1

    As you can see, Category 1 has 1 child category and 1 post. Displayed normally the category page for Category 1 will display all 3 posts under it. However I want to display only only Post 1.1 and Category 1.1, and exclude posts from children categories.

    Any idea how this can be done?

Viewing 3 replies - 1 through 3 (of 3 total)
  • you should choose themes that are so

    [signature moderated Please read the Forum Rules]

    Thread Starter aen

    (@aen)

    Managed to get it working with

    <?php
    // Restrict posts to current category and not its children
    $cat_id = get_query_var('cat');
    $child_cats = (array) get_term_children($cat_id, 'category');
    query_posts(array(
    	'cat' => $cat_id,
    	'category__not_in' => $child_cats
    ));
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    // Display post title, content, etc
    <?php endwhile; endif; ?>

    for displaying non child-category posts.

    That is 1/2 of your problem and also exactly what I am looking for.

    For listing child categories I found this code:

    $cat_id = get_query_var('cat');
    
    $catlist = wp_list_categories('echo=0&orderby=id&title_li=&child_of=' . $cat_id);
    
    if ($catlist) {
    echo $catlist:
    } else {
    // do your loop stuff
    }

    on this post: http://wordpress.org/support/topic/244427?replies=16

    Only problem is that when there are no child categories it prints “no categories” which is ugly and my next task to fix.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Need help, tricky displaying of categories and posts’ is closed to new replies.