Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a site with main categories and subcategories. When you choose a main category, it opens a page with a list of subcategories. When you open a subcategory page it opens a page with a list of posts that belong to that subcategory.

Some subcategory templates don't show the list of posts. Why is that? When I delete the categories and posts and start over it works well, but after 5 test examples it happens again, even with different slugs, category names and posts.

Here is code of my template. I was using a different one before and it also happened with that one.

I think it may be something with the database. Can I fix that somehow?

<?php
if (!category_has_children()) {
        while ( have_posts() ) : the_post(); 
            $categories = get_the_category();
            $last_category = $categories[0];
            foreach($categories as $i => $category)
            {
                if($category->parent == $last_category->cat_ID)
                {
                  $last_category = $category;  
                    the_title();  
                }
            }
        endwhile; 

} else {

    if ( is_category() ) {
        $current_cat = get_query_var('cat');
        $categories=  get_categories('child_of='.get_query_var('cat'));
          foreach($categories as $category) {   
          echo $category->name;
        } 
    } 
?>

And this is the function in functions.php that checks subcategories:

function category_has_children() {
global $wpdb;   
$term = get_queried_object();
$category_children_check = $wpdb->get_results(" SELECT * FROM wp_term_taxonomy WHERE parent = '$term->term_id' ");
    if ($category_children_check) {
        return true;
    } else {
        return false;
    }
}
add_filter( 'init', 'category_has_children' );
share|improve this question
I think that I found what was happening here. There was plugin that we used for adding imagesto categories and if we used the same image for two categories, second category didn't show any post. – Valeka Mar 18 at 18:56

closed as too localized by toscho Apr 2 at 23:43

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.