Category template file for Kadence
Tutorials

Category Archive Template in Kadence Child Theme

Disclosure: This post may contain affiliate links – meaning I get a commission if you decide to make a purchase through my links, at no cost to you.

This tutorial provides the steps to set up a category.php template file that applies to all category archives by using a Kadence child theme for Kadence.

First, let us see how category archives are rendered when Kadence (the parent theme) is active.

As per the WordPress template hierarchy, since there is no category.php present in Kadence theme, WordPress will look for archive.php. Since this file is not present as well, WordPress will begin with index.php in this case.

index.php:

<?php
/**
 * The main archive template file
 *
 * @package kadence
 */

namespace Kadence;

get_header();

kadence()->print_styles( 'kadence-content' );
/**
 * Hook for main archive content.
 */
do_action( 'kadence_archive' );

get_footer();

The relevant line is:

do_action( 'kadence_archive' );

inc/template-hooks.php has:

/**
 * Archive Content
 *
 * @see Kadence\archive_markup();
 */
add_action( 'kadence_archive', 'Kadence\archive_markup' );

So archive_markup function hooked to kadence_archive hook location is run for rendering the archives and this function is defined in inc/template-functions/archive-functions.php like so.

/**
 * Archive Content
 */
function archive_markup() {
	get_template_part( 'template-parts/content/archive', get_post_type() );
}

That is how the code in template-parts/content/archive.php the file is used for the display of all archives.

To have our own individual template file that applies to only the category archives, we need to copy this archive.php into our child theme, rename it as category.php and edit it while adding

get_header();

kadence()->print_styles( 'kadence-content' );

from index.php at the top and

get_footer();

from index.php at the bottom.

Basically you need to sandwich the code from the immediate applicable parent template between calls to header and footer from index.php.

So here’s the final code of category.php:

<?php
/**
 * The main archive template file for inner content.
 *
 * @package kadence
 */

namespace Kadence;

get_header();

kadence()->print_styles( 'kadence-content' );

/**
 * Hook for Hero Section
 */
do_action( 'kadence_hero_header' );
?>
<div id="primary" class="content-area category">
	<div class="content-container site-container">
		<main id="main" class="site-main" role="main">
			<?php
			/**
			 * Hook for anything before main content
			 */
			do_action( 'kadence_before_archive_content' );
			if ( kadence()->show_in_content_title() ) {
				get_template_part( 'template-parts/content/archive_header' );
			}
			if ( have_posts() ) {
				?>
				<div id="archive-container" class="<?php echo esc_attr( implode( ' ', get_archive_container_classes() ) ); ?>">
					<?php
					while ( have_posts() ) {
						the_post();

						get_template_part( 'template-parts/content/entry', get_post_type() );
					}
					?>
				</div>
				<?php
				get_template_part( 'template-parts/content/pagination' );
			} else {
				get_template_part( 'template-parts/content/error' );
			}
			/**
			 * Hook for anything after main content
			 */
			do_action( 'kadence_after_archive_content' );
			?>
		</main><!-- #main -->
		<?php
		get_sidebar();
		?>
	</div>
</div><!-- #primary -->

<?php get_footer(); ?>

From here you can further customize the category archives template as needed.

Any template parts that are called using get_template_part function can be copied from the parent theme to the child theme directory while maintaining the directory structure and customized.

For example, if we take this line:

get_template_part( 'template-parts/content/entry', get_post_type() );

out of the box it will be loading /wp-content/themes/kadence/template-parts/content/entry.php.

If you want to use a modified copy of it, copy it to /kadence/app/public/wp-content/themes/kadence_child/template-parts/content/entry.php where kadence_child is the folder name of the Kadence child theme. After this, category.php created in this tutorial and any other file that calls entry.php will use this modified file.


Reference: https://developer.wordpress.org/reference/functions/get_template_part/#comment-357

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *