WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need help, tricky displaying of categories and posts (4 posts)

  1. Aen
    Member
    Posted 4 years ago #

    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?

  2. phuongly.net
    Member
    Posted 4 years ago #

    you should choose themes that are so

    [signature moderated Please read the Forum Rules]

  3. Aen
    Member
    Posted 4 years ago #

    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.

  4. groomedmonkey
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic