Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created December 7, 2011 20:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/1444580 to your computer and use it in GitHub Desktop.
Save billerickson/1444580 to your computer and use it in GitHub Desktop.
Using a helper function to determine template and body class
<?php
/**
* Return Archive Section
* @author Bill Erickson
* @link http://www.billerickson.net/code/helper-function-for-template-include-and-body-class/
*
* @param null
* @return string
*/
function be_return_archive_section() {
if( is_post_type_archive( 'lifestyle' ) || is_tax( 'lifestyle-section' ) || is_tax( 'lifestyle-type' ) || is_tax( 'lifestyle-series' ) )
return 'lifestyle';
if( is_post_type_archive( 'recipe' ) || is_tax( 'name-range' ) || ( is_tax( 'base' ) && 'recipe' == get_post_type() ) )
return 'recipe';
if( is_post_type_archive( 'brand' ) || ( is_tax( 'base' ) && 'brand' == get_post_type() ) )
return 'brand';
if( is_post_type_archive( 'experience' ) )
return 'experience';
return false;
}
add_filter( 'template_include', 'be_template_chooser' );
/**
* Template Chooser
* Use CPT archive templates for taxonomies
* @author Bill Erickson
* @link http://www.billerickson.net/code/use-same-template-for-taxonomy-and-cpt-archive/
*
* @param string, default template path
* @return string, modified template path
*
*/
function be_template_chooser( $template ) {
if ( be_return_archive_section() )
$template = get_query_template( 'archive-' . be_return_archive_section() );
return $template;
}
add_filter( 'body_class', 'be_section_body_classes' );
/**
* Section Body Classes
* @author Bill Erickson
*
* @param array $classes
* @return array
*/
function be_section_body_classes( $classes ) {
if( be_return_archive_section() )
$classes[] = 'section-' . be_return_archive_section();
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment