Default Archive Title

See: Automatically display titles on Category and Tag Archives

<?php

/**
 * Default Titles for Term Archives
 *
 * @author Bill Erickson
 * @see https://www.billerickson.net/default-category-and-tag-titles
 *
 * @param string $value
 * @param int $term_id
 * @param string $meta_key
 * @param bool $single
 * @return string $vlaue
 */
function ea_default_term_title( $value, $term_id, $meta_key, $single ) {

	if( ( is_category() || is_tag() || is_tax() ) && 'headline' == $meta_key && ! 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', 10 );
		$value = get_term_meta( $term_id, 'headline', true );
		add_action( 'get_term_metadata', 'ea_default_term_title', 10, 4 );

		// Use term name if empty
		if( empty( $value ) ) {
			$term = get_queried_object();
			$value = $term->name;
		}
	
	}

	return $value;		
}
add_filter( 'get_term_metadata', 'ea_default_term_title', 10, 4 );

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk