Tips

Custom Taxonomy Friendly Archives

Whereas not all themes make the most of customized taxonomies as a part of the theme itself (maybe for portfolios), it’s completely very important that every one themes embrace full assist for customized taxonomy archives. What I see far too usually are themes that don’t adequately assist customized taxonomies of their archive templates and find yourself inflicting a PHP error to happen when viewing a taxonomy archive. This fast tutorial goes to point out you a very easy solution to keep away from this downside and supply full assist for customized taxonomies, all in a single template file.

This tutorial goes to imagine that every one class, tag, and taxonomy archives are displayed together with your single archive.php file, versus separate recordsdata for class.php, tag.php, and many others.

More often than not, on the prime of the archive.php file, theme builders will place one thing like this:

<h2><?php echo single_cat_title(); ?></h2>
<div class="category-description"><?php echo category_description(); ?></div>

Or this:

<h2><?php echo single_tag_title(); ?></h2>
<div class="tag-description"><?php echo tag_description(); ?></div>

Or a mix:

<?php if( is_category() ) { ?>
	<h2><?php echo single_cat_title(); ?></h2>
	<div class="category-description"><?php echo category_description(); ?></div>
<?php } elseif( is_tag() ) { ?>
	<h2><?php echo single_tag_title(); ?></h2>
	<div class="tag-description"><?php echo tag_description(); ?></div>
<?php } ?>

These are all completely fantastic features to make use of, besides that they don’t work with customized taxonomies and can truly lead to an error being thrown (if not utilizing an is_category/is_tag conditional).

If we take a look at the supply code for the tag_description() perform, we’ll see that it’s truly only a reference to the same term_description() perform, however with the “post-tag” parameter set as default.

/**
 * Retrieve tag description.
 *
 * @since 2.8
 *
 * @param int $tag Non-compulsory. Tag ID. Will use international tag ID by default.
 * @return string Tag description, accessible.
 */
perform tag_description( $tag = 0 ) {
        return term_description( $tag );
}

The term_description() perform is very nice as a result of (if we take a look at the supply), we see that it’ll truly detect the present taxonomy sort that’s being displayed and show the outline for the time period from the correct taxonomy, as an alternative of throwing the error like tag_description() does. See the perform beneath:

/**
 * Retrieve time period description.
 *
 * @since 2.8
 *
 * @param int $time period Non-compulsory. Time period ID. Will use international time period ID by default.
 * @param string $taxonomy Non-compulsory taxonomy title. Defaults to 'post_tag'.
 * @return string Time period description, accessible.
 */
perform term_description( $time period = 0, $taxonomy = 'post_tag' ) {
        if ( !$time period && ( is_tax() || is_tag() || is_category() ) ) {
                $time period = get_queried_object();
                $taxonomy = $time period->taxonomy;
                $time period = $time period->term_id;
        }
        $description = get_term_field( 'description', $time period, $taxonomy );
        return is_wp_error( $description ) ? '' : $description;
}

There’s additionally an important perform for the taxonomy time period title that features the identical means: single_term_title()

This all implies that we are able to merely use the next in our archive.php and it’ll work for all taxonomies:

<h2><?php echo single_term_title(); ?></h2>
<div class="tax-description"><?php echo term_description(); ?></div>

That’s a lot better than creating a number of recordsdata to take care of the totally different taxonomies (except you’re creating customized layouts for every) and can also be a lot safer.

Download The WPSaviour App Now

Leave a Reply

Your email address will not be published.

Pay in your preferred currency
Indian rupee
Euro