1

I'm looking to update my category template to display subcategory posts differently than category posts. I'm fairly new to Wordpress template editing but know HTML/CSS well, and PHP enough to understand it.

Currently, my theme is displaying all category & subcategory posts with titles, thumbnails, and excerpts with a button. But, I do not want subcategory posts to display like this from the main category page. I'd like to instead display categories as: subcategory title, subcategory description, button --> all of which links to the subcategory page. Then, when viewing the subcategories at their own level, my theme has a separate template that will kick in.

I'm working from a child of the AccessPress Lite theme. I've duplicated their existing archive.php and renamed it to category.php. I've also duplicated the loop that was being used to display the content, but am now struggling to figure out the logic behind determining if it's a category/subcategory.

My loop is currently:

<?php while ( have_posts() ) : the_post(); ?>
<?php
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="entry-header">
            <h1 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h1>

            <?php if ( 'post' == get_post_type() ) : ?>
            <div class="entry-meta">
                <?php accesspresslite_posted_on(); ?>
            </div><!-- .entry-meta -->
            <?php endif; ?>
        </header><!-- .entry-header -->

        <?php if ( is_search() ) : // Only display Excerpts for Search ?>
        <div class="entry-summary">
            <?php the_excerpt(); ?>
        </div><!-- .entry-summary -->
        <?php else : ?>
        <div class="entry-content">
            <?php if(has_post_thumbnail()){?>
            <div class="entry-thumbnail">
                <?php  the_post_thumbnail('thumbnail'); ?>
            </div>
            <?php } ?>
            <div class="short-content">
            <?php echo accesspresslite_excerpt( get_the_content() , 500 ) ?>
            </div>
            <a href="<?php the_permalink(); ?>" class="bttn"><?php _e('More','accesspresslite')?></a>
            <?php
                wp_link_pages( array(
                    'before' => '<div class="page-links">' . __( 'Pages:', 'accesspresslite' ),
                    'after'  => '</div>',
                ) );
            ?>
        </div><!-- .entry-content -->
    <?php endif; ?>

        </article><!-- #post-## -->
<?php endif; ?>
                ?>

1 Answer 1

0

You will need a simple check inside your loop which logic is quite also simple. The logic behind this is as follow:

  • If a post belongs to the current queried category (AKA the parent category), it will have this category attached to it

  • If a post belongs to only a child category of the current queried category, then it will not have the current queried category attached to it.

With this is mind, we only then need to determine if the current post have the queried category assigned to it. For this, we can use the conditional check, has_category(), to determine if the post has our desired catgeory or not, and then use get_queried_object_id() to get the ID of the current category beign viewed

Inside your loop, you can do something like the following:

$current_category_id = get_queried_object_id();
if ( has_category( $current_category_id ) ) { // Post belongs to the queried category
    // Add your specific markup and code for current queried category
} else { // Post belongs to a child category
    // Add your specific markup and code for child categories
}
3
  • Thank you Pieter, this is exactly what I was looking for! Works like a charm :)
    – Pandy
    Commented Feb 25, 2016 at 2:16
  • One comment though - your variable name in has_category() doesn't match the original declaration. It wouldn't let me make the edit cause it was under the 6 char limit :)
    – Pandy
    Commented Feb 25, 2016 at 2:18
  • Glad it worked. Thanks for the bug report, these silly mistakes happens when one post from mobile devices. Just a tip, in a case like this, if the edit is too short, just add any comment into the code to make up the silly 6 character limit ;-) Commented Feb 25, 2016 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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