Show Category/Taxonomy Description on NON-Archive Pages via Shortcode

From IThemes Codex
Revision as of 16:35, July 22, 2013 by Elise (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Category Descriptions are a little more sophisticated than other post related variables because if there is more than one category being used, then it will be hard for WordPress to display ALL descriptions nicely OR one category (which one?).

  • Outside of archive pages, to display Category Descriptions (or any taxonomy description) you have to do the following:
    • 1 : Put the following anywhere (usually near the end of the file) in your theme's custom functions.php file (it creates a shortcode you'll use to display the description of a Category or any Taxonomy):
add_action( 'init', 'lb_cat_init' );
function lb_cat_init() {
	add_shortcode('taxdescription', 'lb_the_description');
} //end lb_cat_init
function lb_the_description() {
	global $post;
	if ( !is_object( $post ) ) return;
	$terms = get_post_meta( $post->ID, 'description_terms', true );
	$taxonomy = get_post_meta( $post->ID, 'description_taxonomy', true );
	$return = '';
	if ( $terms && $taxonomy ) {
		if ( !is_array( $terms ) ) {
			$terms = explode( ',', $terms );
		}
		foreach ( $terms as $term_name ) {
			$term = get_term_by( 'slug', $term_name, $taxonomy );
			if ( !$term ) continue;
			$description = get_term_field( 'description', $term->term_id, $term->taxonomy );
			if ( empty( $description ) || !$description ) continue;
			$return .= apply_filters( 'pb_the_content', $description );
		}
	}
	return $return;
} //end lb_the_description
    • 2 : In the posts you want to show the category description, add the following 2 custom field names and values:

Custom Field Name ----- Custom Field Value

description_taxonomy ----- category

description_terms ----- books (OR any category name you have)

    • 3 : Add a Shortcode Tag to your LoopBuddy layout which has the following shortcode in it:

[taxdescription]

That will show the category description anywhere you put the shortcode.


← Back to LoopBuddy Codex Home

Personal tools
Namespaces
Variants
Actions
iThemes Codex
Codex Navigation
Toolbox