WordPress.org

Ready to get started?Download WordPress

Forums

Category template dont show posts from SOME categories (1 post)

  1. ValekaVale
    Member
    Posted 1 year ago #

    I need help please. I have site with main categories and sub categories. When you choice main category it opens page with list of subcategories. When you open sub category page it opens page with list of post that belongs to that subcategory.

    I used the same category template for this and it works great, but for some subcategories template dont show list of posts. How is that? When I delete categories and post and start over it works well, but after 5 test examples it happened again with different slugs, category names and posts.

    Here is code of my template, but I was using different one before and it also happened. I think it can be something about database. Can I fix that somehow? And as I'm working with client from other country,I noticed that it always happened when he adds categories and posts.

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

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

Topic Closed

This topic has been closed to new replies.

About this Topic