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' );