• I’ve been cracking my brains on a problem, probably a stupid mistake I’m not seeing.
    On category page for a specific category, in this case “Interview” I want to display the posts of the child categories of “Interview”. So category page would have two loops; one for posts from “Interview” and one in the sidebar for posts from child categories of “Interview”.

    I tried several codes but none works in the sidebar, I just get a blank sidebar. See code: https://pastebin.com/3ie75KSf If I echo some test text between the foreach loop it won’t print. Which means the foreach loop does not work. I can’t figure what I’m doing wrong.

    Anyone any ideas?

Viewing 11 replies - 1 through 11 (of 11 total)
  • You probably tried, what does count($descendants) give you?

    Edit: reading your code, I see a file include inside a for loop. I guess it sets the $cat value. Seems to me it’s better to create a function and call that function inside the loop. If only for disk i/o concerns.

    Anything in the error log?

    Thread Starter daricedotorg

    (@daricedotorg)

    Should have thought about trying count. It returns 0 even though “Interview” has 1 child category with several posts. Now I need to figure why it won’t get the child category.

    The site isn’t really heavy traffic, around 1800 hits a day but I’ll look into making a function instead of file include, thanks for the tip.

    First thing i’d check is the parameters in

    $catPosts->query("showposts=3&cat=$child->term_id");

    the showposts parameter has been replaced by posts_per_page, yet should work. Maybe it’s a combination of the parameters that’ll do the trick.

    Thread Starter daricedotorg

    (@daricedotorg)

    I zeroed in the problem to this line:

    $descendants = get_categories(array('child_of' => 11));

    It works fine when I try another category that has lots of children but it won’t work with category 11(Interview), it keeps returning an empty result. This category has one child and that child has 7 posts.

    I tried adding both the parent and child category to these posts, but it still returns 0 result. So, I’m at lost why the code works just fine with other categories but not with the intended category.

    Parent category is id 11 and has 57 posts, child category is id 115 and has 7 posts. In the future the parent category will get more children with each their own posts.

    From your initial post, it seems you have a custom post type Interview. Is this correct? In that case, the category should be registered for this post type. Otherwise it will only work for posts.

    For more info on custom taxonomies, see

    https://codex.www.ads-software.com/Taxonomies#Custom_Taxonomies

    Thread Starter daricedotorg

    (@daricedotorg)

    No, it’s regular posts with their categories. Nothing out of the usual.

    The main page is category.php, see https://pastebin.com/rCSP51B6

    You’ll notice I have an if/else in place for assigning the sidebar. When viewing the “Interview” category(id=11) it should get the sidebar with code to get the posts of the child categories of “Interview”.

    Like said, the code works fine when I add another category in this line

    $descendants = get_categories(array('child_of' => 11)); category “Column”(id=69) for example does return results. So there is something off with category 11 and its child category 115

    Thread Starter daricedotorg

    (@daricedotorg)

    I have narrowed the problem down to a database issue.

    When I use this code:

    <?php  $this_cat = get_query_var('cat');  ?>
    
    	<ul>
    <?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=' . $this_cat . ''); ?>
    	</ul>

    It works correct for all categories except for category named “Interview” which contains one child category.

    I can’t figure what the problem could be. The data in wp_term_taxonomy seems correct. The child category has the right id corresponding to “interview” as parent id.

    I even created a new category Interview and deleted the old one. The problem still persists.

    Does the cild category contain any posts?
    If not, use the hide_empty parameter: https://codex.www.ads-software.com/Function_Reference/get_categories#Parameters

    Thread Starter daricedotorg

    (@daricedotorg)

    Screenshot gives me a 403. But I believe you ??
    I’m clueless at this point..

    Thread Starter daricedotorg

    (@daricedotorg)

    Well this is clearly a bug in WordPress.

    I tried the Twenty Fourteen Theme and that one also did not show the “Interview” category and its child with wp_list_categories()

    I finally solved it by creating a new child category and assigned it the same posts as the existing child category and the categories started showing. I deleted the newly created child category and the category and child is still showing.

    Now this creates another problem: when viewing all posts in the parent category in the wordpress admin it also shows all posts form the child category. On https://mysite.com/category/interview/ same thing. It will list all post in “Interview” and also posts in its child “De lector en het nieuws”.

    What a headache.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Second loop in sidebar on category page not working’ is closed to new replies.