WP Knowledge Base Theme bug – Subcategories and Articles, Need to change WP_Query

H’okay, so, using this resource, I was found that going into the template-knowledgebase.php file and replacing the cat keyword with category__in (that’s a double underscore) did only return the articles who’s parent were the category we were looking at. I also edited the else statement of the if to be endif and removed the extraneous endif. This is what my list-group div wound up looking like.

<div class="list-group"> 
              <?php /* list-group fix - jfa*/ ?>
                            <?php if ( ! empty( $sub_categories ) ) : ?>
                            <?php foreach ( $sub_categories as $scat ) : ?>
                            <?php $sterm_meta = get_option( 'ipt_kb_category_meta_' . $scat->term_id, array() ); ?>
                            <a href="https://wordpress.stackexchange.com/questions/154208/<?php echo esc_url( get_term_link( $scat,"category' ) ); ?>" class="list-group-item">
                                <span class="badge"><?php echo $scat->count; ?></span>
                                <?php if ( isset( $sterm_meta['icon_class'] ) && '' != $sterm_meta['icon_class'] ) : ?>
                                <i class="glyphicon <?php echo esc_attr( $sterm_meta['icon_class'] ); ?>"></i>
                                <?php else : ?>
                                <i class="glyphicon ipt-icon-books"></i>
                                <?php endif; ?>
                                <?php echo $scat->name; ?>

                            </a>
                            <?php endforeach; ?>
                            <?php endif; ?>
                            <?php $cat_posts = new WP_Query( array(
                                'posts_per_page' => get_option( 'posts_per_page', 5 ),
                                'category__in' => $cat->term_id, // 'cat' changed to 'category__in' - jfa
                            ) ); ?>
              <?php /* ze loop - jfa */ ?>
                            <?php if ( $cat_posts->have_posts() ) : ?>
                            <?php while ( $cat_posts->have_posts() ) : $cat_posts->the_post(); ?>
                                <?php get_template_part( 'category-templates/content', 'popular' ); ?>
                            <?php endwhile; ?>
                            <?php elseif ( empty( $sub_catagories ) ) : ?>
                                <?php get_template_part( 'category-templates/no-result' ); ?>
                            <?php endif; ?>
              <?php /* end loop - jfa */ ?>
                            <?php wp_reset_query(); ?>
                            <?php /*endif;*/ ?>
                        </div><?php /* end list-group - jfa*/ ?>

Then I went into category-templates/category-parent.php and added this after the endif

<?php /* start article section */ ?>

<?php $sterm_meta = get_option( 'ipt_kb_category_meta_' . $scat->term_id, array() ); ?>
    <?php $sterm_link = esc_url( get_category_link( $scat ) ); ?>
    <?php $scat_totals = $post/*ipt_kb_total_cat_post_count( $scat->term_id )*/; ?>

<!-- source of issue on second page -->
    <?php $scat_posts = new WP_Query( array(
        'category__in' => $cat->term_id,
        'posts_per_page' => get_option( 'posts_per_page', 5 ),
    ) ); ?>
<!-- end source of issue on second page -->
<!-- <?php echo $scat->term_id; ?> -->

<!-- this is the box -->
    <div class="col-md-6 kb-subcat">
        <div class="col-md-3 col-sm-2 hidden-xs kb-subcat-icon">
            <p class="text-center">
                <a href="https://wordpress.stackexchange.com/questions/154208/<?php echo $sterm_link; ?>" class="thumbnail">
                    <i class="glyphicon ipt-icon-file"></i>
                </a>
            </p>
        </div>
        <div class="col-md-9 col-sm-10 col-xs-12">
            <h2 class="knowledgebase-title"><a data-placement="bottom" data-popt="kb-homepage-popover-<?php echo $scat->term_id; ?>" title="<?php echo esc_attr( sprintf( __( '%1$s / %2$s', 'ipt_kb' ), $cat->name, $scat->name ) ); ?>" href="#" class="btn btn-default btn-sm text-muted ipt-kb-popover"><i class="glyphicon ipt-icon-paragraph-justify2"></i></a> <?php echo $article_heading; ?></h2>
            <div class="ipt-kb-popover-target" id="kb-homepage-popover-<?php echo $scat->term_id; ?>">
                <?php echo wpautop( $scat->description ); ?>
                <p class="text-right">
                    <?php if ( isset( $term_meta['support_forum'] ) && '' != $term_meta['support_forum'] ) : ?>
                    <a class="btn btn-default" href="<?php echo esc_url( $term_meta['support_forum'] ); ?>">
                        <i class="glyphicon ipt-icon-support"></i> <?php _e( 'Get support', 'ipt_kb' ); ?>
                    </a>
                    <?php endif; ?>
                    <a href="https://wordpress.stackexchange.com/questions/154208/<?php echo $sterm_link; ?>" class="btn btn-info">
                        <i class="glyphicon ipt-icon-link"></i> <?php _e( 'Browse all', 'ipt_kb' ); ?>
                    </a>
                </p>
            </div>
            <!--Begin Books Icon for Categories -->
            <div class="visible-xs kb-subcat-icon">
                <p class="text-center">
                    <a href="https://wordpress.stackexchange.com/questions/154208/<?php echo $sterm_link; ?>" class="thumbnail">
                        <?php if ( isset( $sterm_meta['icon_class'] ) && '' != $sterm_meta['icon_class'] ) : ?>
                        <i class="glyphicon <?php echo esc_attr( $sterm_meta['icon_class'] ); ?>"></i>
                        <?php else : ?>
                        <i class="glyphicon ipt-icon-books"></i>
                        <?php endif; ?>
                    </a>
                </p>
            </div>
    <!-- End Books Icon -->

    <!-- Beign Category Section -->
            <div class="list-group">
                <?php if ( $scat_posts->have_posts() ) : ?>
                    <?php while ( $scat_posts->have_posts() ) : $scat_posts->the_post(); ?>
                    <?php get_template_part( 'category-templates/content', 'popular' ); ?>
                    <?php endwhile; ?>
                <?php else : ?>
                    <?php get_template_part( 'category-templates/no-result' ); ?>
                <?php endif; ?>
            </div>
    <!-- End Category Section -->

    <!--Begin Browse Articles Button -->
    <p class="text-right">
                <a class="btn btn-default" href="https://wordpress.stackexchange.com/questions/154208/<?php echo $sterm_link; ?>"><i class="glyphicon ipt-icon-link"></i> <?php printf( _n( 'Browse %d article', 'Browse all %d articles', $scat_totals, 'ipt_kb' ), $scat_totals ); ?></a>
            </p>
    <!-- End Browse Articles Button -->
        </div>
        <div class="clearfix"></div>
    </div>
<!-- end box -->
    <?php wp_reset_query(); ?>
<?php /* end article */ ?>

This basically creates a box for any spare articles, based on the subcategory box. Underneath the global statement, be sure to add $article_heading = "Articles";. This names the new box Articles, although you can call it whatever you want.

It’s not a totally robust solution; for example, the number of posts in each category will go to posts per category * 2, so I may come back to this. There also may be a more efficient way, but this theme has not been supported in a while, so this is the slightly hacky fix that I have.

Alternately, a completely robust workaround that doesn’t involve changing any code is to put everything in subcategories, but only to one level. This theme does not support categories beyond one level. It mitigates the entire situation, and isn’t really that hard.

I’ll post any future changes, just so that there is documentation for this.