0

I'm using the WP Knowledge Base theme, which is almost perfect for the project I'm on, but I discovered an unfortunate bug. On the home page, if you have both SubCategories and loose Articles under a single Category, it won't display the Articles along side the SubCategories. You can see in the following picture how on the left, a single article is displayed that category, but on the right, two subcategories are shown, when the top category should have two sub categories and an article

SubCategories, no article

The inside of the category on the right looks like the following picture. Notice that although there are 4 articles listed, only 3 are present under subcategories, and there is no fourth article.

No fourth article

I went through the code and found this section:

<div class="list-group">
    <?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="<?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 else : ?>
     <!-- so get the number of posts per page(UD) -jfa -->
    <?php $cat_posts = new WP_Query( array(
    'posts_per_page' => get_option( 'posts_per_page', 5 ),
    'cat' => $cat->term_id,
    ) ); ?>
    <?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 else : ?>
        <?php get_template_part( 'category-templates/no-result' ); ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>
    <?php endif; ?>
</div><!--end list-group - jfa-->

This is the section that defines what populates that list. I changed the else statement of the initial if to an endif and took out the endif at the very end, now the SubCategories and Articles being displayed are no longer exclusive. But, it displays all the articles, that is all the leaves, alongside the SubCategories. I.e., it displays all the subcategories alongside the articles that are in those SubCategories. In order to fix this, I have to change the WP_Query, I think, so that it only returns articles in the current category and not the Subcategories.

I'm looking at the documentation currently, but does anyone have any ideas for this? I'm not as familiar with this as I'd like to be.

Edit:

A working example of this theme can be found on their site

Edit 2:

Ok, I'm really not getting this query. Here is a closeup.

<?php $cat_posts = new WP_Query( array(
  'posts_per_page' => get_option( 'posts_per_page', 5 ),
  'cat' => $cat->term_id,
  ) ); ?>

So posts per page gets the option that I set in WordPress. 'cat' => $cat->term_id looks like the same one that is on the examples page. $cat->term_id returns the particular category number. Unfortunately, it's also returning all the articles in the subcategories that are in that category. Is there a way to change that so it only returns articles at that level and not levels below?

0

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="<?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="<?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="<?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="<?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="<?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.

  • Hello @JFA, have you had a luck finding a robust solution? Thanks! – Netizen Jan 27 '17 at 20:48
  • 1
    @iamsj I wound up having to fix the bug by hand. I can review my code, but I believe this has the correct solution to the issue. – JFA Jan 28 '17 at 1:00
  • There is no template-knowledgebase.php file inside this plug-in folder. – PolGraphic Jun 11 '17 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.