Skip to content

Instantly share code, notes, and snippets.

@theMikeD
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theMikeD/412c5fe009876f653701 to your computer and use it in GitHub Desktop.
Save theMikeD/412c5fe009876f653701 to your computer and use it in GitHub Desktop.
Indicate which pages are taxonomy archives
add_filter( 'display_post_states', 'cnmd_add_archive_page_indicators', 10, 2);
/**
* cnmd_add_archive_page_indicators adds post state labels for custom and builtin taxonomy
* archive pages
*
* @param ARRAY $post_states
* @param POST OBJ $post
*
* @return array
*/
function cnmd_add_archive_page_indicators($post_states, $post) {
// Get the taxonomy slug, since this is what will be used to find the archive page
$slug = $post->post_name;
if ( ! $slug ) {
return $post_states;
}
// Get the name of each custom public registered taxonomy
$args = array(
'public' => true,
'_builtin' => false,
);
$custom_taxonomies = get_taxonomies( $args, 'objects' );
$new_post_state = false;
if ( ! empty( $custom_taxonomies ) ) {
$new_post_state = cnmd_get_archive_page_post_state( $custom_taxonomies, $slug );
if ( $new_post_state ) {
$post_states[] = $new_post_state;
}
}
if ( ! $new_post_state ) {
// Get the name of each built in registered taxonomy
$args = array(
'public' => true,
'_builtin' => true,
);
$builtin_taxonomies = get_taxonomies( $args, 'objects' );
if ( ! empty( $builtin_taxonomies ) ) {
$new_post_state = cnmd_get_archive_page_post_state( $builtin_taxonomies, $slug );
if ( $new_post_state ) {
$post_states[] = $new_post_state;
}
}
}
return $post_states;
}
/**
* cnmd_get_archive_page_post_state returns a string if $slug matches any of the supplied $taxonomies slugs,
* which indicates that the page is the archive page for that taxonomy.
*
* @param ARRAY $taxonomies Array of taxonomy info objects
* @param STRING $slug Slug of current page
*
* @return string
*/
function cnmd_get_archive_page_post_state( $taxonomies, $slug ) {
if ( empty ( $taxonomies )) {return;}
if ( '' === $slug ) { return; }
foreach ( $taxonomies as $taxonomy ) {
if ( empty ( $taxonomy->rewrite ) || ! array_key_exists('slug', $taxonomy->rewrite) || '' === $taxonomy->rewrite[slug]) {continue;}
if ( $slug === $taxonomy->rewrite[ slug ] ) {
return "Archive page for " . $taxonomy->labels->name ;
}
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment