• Hi,

    How can I show just 1 post in the parent category and all the other posts (excluding the one showing in the parent category) in the child category? I’m breaking my head trying to figure this out and need some help.

    Thanks

Viewing 15 replies - 1 through 15 (of 33 total)
  • Can you please give some more details? Is the only association these posts have the fact that their in the same category? What is the relevance of the single post in the parent category to the many posts in the child category? I assume you’re using WP_Query for this.

    Thread Starter amandach

    (@amandach)

    So I’m actually using fold category list because I want a category tree but the php is totally customizable and you can also use wordpress codex to add args.

    I have 1 post that’s in the parent category and then all other posts are in the child category only. Although the parent category is not checked off (just the child cats are) the posts are showing up when you click on the parent category.

    For example, in Post 1, Parent Cat 1 is checked off. Post 1 shows up in Parent Cat 1. Post 2 is in Child Cat 1 (child cat of Parent Cat 1) but it’s showing up in Parent Cat 1 AND Child Cat 1. I need it to show up in Child Cat 1 ONLY not Parent Cat 1.

    Thanks for your help.

    Cheers,
    Amanda

    Hmm, I’ve never used the folder category list thing you speak of. Let me see if I understand your problem though (let me know if this isn’t correct). You are viewing posts on a category listing page (something like yoursite.com/category/parent-cat-1). On that page you only want Post 1 to show up (the post that’s directly in the parent-cat-1 category) but posts from it’s child categories are also showing up?

    Would you happen to have some demo pages you could link to that demonstrate this?

    Thread Starter amandach

    (@amandach)

    Yes that’s spot on! The site is on my localhost so I can’t post ??
    Is there any arg that I can add to the codex that will make that work? Or it’s more complicated?

    The fold category list isn’t really relevant because I can add any custom php to that.

    I can only show you the WP_Query argument because that’s what I use. You can exclude posts from a particular category. In this case, you’d want to exclude all posts from that child category.

    If Parent Cat 1 has a category ID of 1 and
    Child Cat 1 has a category ID of 2 then:

    'category__not_in' => array( 2 )

    would be the parameter for WP_Query. You can view the category parameters for WP_Query here.

    You could pass additional arguments to that array if you wanted to exclude other categories as well.

    Thread Starter amandach

    (@amandach)

    So there’s no general rule that I can do ALL child cats? I don’t want my client to have to go into the php each time he creates a new child cat. Is there any possible way to just limit all parent categories to 1 post? In other words, to exclude all child cats from appearing in the parent cat?

    Sure, you could do that. You’d just need first get all the child categories, organize them into an array of id’s, then pass that to your query.

    You could use get_categories and use its child_of parameter (passing the parent category’s id to there).

    After that, there are several ways to get just the id’s into an array. You might try looping the categories array and creating a new array from just the ids like this:

    $categories = get_categories( $your_args_here );
    $exclude_these = array();
    foreach ( $categories as $category ) {
    	$exclude_these[] = $category->cat_ID;
    }
    
    // at this point, $exclude_these is a proper array  of
    // child categories that can be passed to your query
    Thread Starter amandach

    (@amandach)

    Don’t I need to know the category ids this way? Is there any way to do it without having to specify the id’s? Like I said, I don’t want my client to have to go into the php each time he creates a new category.

    You only need to know the parent category’s ID in order for that method to work (which should be one of wp_query’s default options on the category view page). Try globalizing wp_query and then printing it and see what’s in there:

    On category template:

    global $wp_query;
    echo '<pre>' . print_r( $wp_query, true ) . '</pre>';

    You can also try doing that with the $query_string global. If you can find the category slug you can get the ID from that through one of WordPress’s functions.

    Thread Starter amandach

    (@amandach)

    Is there a plugin that can do this automatically? Because my client will have to do this again for a new parent category he adds.

    There probably are plugins, but that’s what I was saying about gathering the category from wp_query.

    Whenever you go to a category detail page like this:
    yoursite.com/category/category-name

    WP_Query automatically knows some things about that page (that’s how it displays the categories to begin with). You can use that slug that’s passed in to get the category id of whatever category is currently being viewed. Because of that, the code will work dynamically for all categories.

    If you’d like, I can write up a working prototype for you. ??

    Thread Starter amandach

    (@amandach)

    Jacob that would be awesome if you can write something up for me. I’m not really getting it.

    You rock!

    Amanda

    Here you go ??
    I tested this on my test blog; it’s fully functional code.

    https://pastebin.com/MfUbMwti

    Thread Starter amandach

    (@amandach)

    Great I’ll get this on the site and let you know if it works!! Thanks so much!

    No problem; hope it works out for you!

    Remember, that code goes in the category.php template ??

Viewing 15 replies - 1 through 15 (of 33 total)
  • The topic ‘show 1 post in parent category’ is closed to new replies.