Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created Jan 28, 2017
Embed
What would you like to do?
<?php
/**
* Default Titles and Descriptions for Term Archives
*
* @author Bill Erickson
* @see http://www.billerickson.net/default-category-and-tag-titles
*
* @param string $headline
* @param object $term
* @return string $headline
*/
function ea_default_term_title_desc( $value, $term_id, $meta_key, $single ) {
if( ( is_category() || is_tag() || is_tax() ) && in_array( $meta_key, array( 'headline', 'intro_text' ) && ! is_admin() ) {
// Grab the current value, be sure to remove and re-add the hook to avoid infinite loops
remove_action( 'get_term_metadata', 'ea_default_term_title_desc', 10 );
$value = get_term_meta( $term_id, $meta_key, true );
add_action( 'get_term_metadata', 'ea_default_term_title_desc', 10, 4 );
// Use term name/desc if empty
if( empty( $value ) ) {
$term = get_term_by( 'term_taxonomy_id', $term_id );
$value = 'headline' == $meta_key ? $term->name : $term->description;
}
}
return $value;
}
add_filter( 'get_term_metadata', 'ea_default_term_title_desc', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment