Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:08
-
Save scofennell/f8f52b89a004c58ef4b2 to your computer and use it in GitHub Desktop.
WordPress function to create a breadcrumb nav.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Wrap the crumb. | |
* @see https://schema.org/breadcrumb | |
*/ | |
if( ! function_exists( 'anchorage_get_breadcrumb' ) ) { | |
function anchorage_get_breadcrumb( $crumb_title, $crumb_link ) { | |
$crumb_title = wp_kses_post( $crumb_title ); | |
$crumb_link = esc_url( $crumb_link ); | |
$out = "<span itemprop='title'>$crumb_title</span>"; | |
// Unless it's a crumb to the current page, link it. | |
if( ! empty( $crumb_link ) ) { | |
$out = "<a href='$crumb_link' class='breadcrumbs-link' itemprop='url'>$out</a>"; | |
} | |
return $out; | |
} | |
} | |
/** | |
* Determine if a post type supports a taxonomy. | |
* | |
* @param string $post_type The name of a post type. | |
* @return boolean Returns true if the given post type supports the given taxonomy, else false. | |
*/ | |
if( ! function_exists( 'anchorage_post_type_has_tax' ) ) { | |
function anchorage_post_type_has_tax( $post_type, $taxonomy_name ) { | |
if( ( $post_type == 'post' ) && ( ( $taxonomy_name == 'post_tag' ) || ( $taxonomy_name == 'category' ) ) ) { return true; } | |
$post_type_object = get_post_type_object( $post_type ); | |
$taxonomies = $post_type_object -> taxonomies; | |
if( empty( $taxonomies ) ) { return false; } | |
if( in_array( $taxonomy_name, $taxonomies ) ) { | |
return true; | |
} | |
return false; | |
} | |
} | |
/** | |
* Determine if a post type is custom or not. | |
* | |
* @param string $post_type The name of a post type. | |
* @return boolean Returns true if the given post type is custom, else false. | |
*/ | |
if( ! function_exists( 'anchorage_is_custom_post_type' ) ) { | |
function anchorage_is_custom_post_type( $post_type ) { | |
$all_custom_post_types = get_post_types( array ( '_builtin' => FALSE ) ); | |
// there are no custom post types | |
if ( empty ( $all_custom_post_types ) ) { | |
return false; | |
} | |
$custom_types = array_keys( $all_custom_post_types ); | |
if( in_array( $post_type, $custom_types ) ) { | |
return true; | |
} | |
return false; | |
} | |
} | |
/** | |
* Determine if the current view is tax, tag, or cat. | |
* | |
* @return boolean Returns true if current view is tax, tag, or cat, otherwise false. | |
*/ | |
if( ! function_exists( 'anchorage_is_termish' ) ) { | |
function anchorage_is_termish() { | |
if( is_tax() || is_tag() || is_category() ) { | |
return true; | |
} | |
return false; | |
} | |
} | |
/** | |
* Determine if the current view is single, singular, or page. | |
* | |
* @return boolean Returns true if is single, singular, or page, otherwise false. | |
*/ | |
if( ! function_exists( 'anchorage_is_singlish' ) ) { | |
function anchorage_is_singlish() { | |
if( is_single() || is_singular() || is_page() ) { | |
return true; | |
} | |
return false; | |
} | |
} | |
/** | |
* Get an HTML <hr /> with classes expected by our stylesheet. | |
* | |
* @param array $classes An array of HTML classes. | |
* @return string An HTML <hr /> with classes expected by our stylesheet. | |
* | |
* @since anchorage 1.0 | |
*/ | |
if( ! function_exists( 'anchorage_get_hard_rule' ) ) { | |
function anchorage_get_hard_rule( $classes = array() ) { | |
$classes = array_map( 'sanitize_html_class', $classes ); | |
$classes []= 'break'; | |
$classes_str = implode( ' ', $classes ); | |
$out = "<hr class='$classes_str' />"; | |
return $out; | |
} | |
} | |
/** | |
* Grab the unicode char for an arrow in a given direction, with classes and an href. | |
* | |
* @param string $direction The direction in which the arrow will point. | |
* @param array $classes An array of HTML classes for the arrow. | |
* @param string $href Link the arrow to a url. | |
* @return string The arrow, with classes, wrapped in either a link or span. | |
* | |
* @since anchorage 1.0 | |
*/ | |
if( ! function_exists( 'anchorage_get_arrow' ) ) { | |
function anchorage_get_arrow( $direction = 'down', $classes = array(), $href = '#' ) { | |
// Grab the correct arrow char for the direction. | |
if ( $direction == 'left' ) { | |
$out = esc_html__( '←', 'anchorage' ); | |
} elseif ( $direction == 'up' ) { | |
$out = esc_html__( '↑', 'anchorage' ); | |
} elseif ( $direction == 'right' ) { | |
$out = esc_html__( '→', 'anchorage' ); | |
} else { | |
$out = esc_html__( '↓', 'anchorage' ); | |
} | |
// Build the classes. | |
$classes = array_map( 'sanitize_html_class', $classes ); | |
$classes = implode( ' ', $classes ); | |
$classes = " class='$classes arrow' "; | |
$out = " $out "; | |
// If there's an href, wrap the arrow in a link. | |
$href = esc_attr( $href ); | |
if( ! empty( $href ) ) { | |
$out = "<a href='$href' $classes>$out</a>"; | |
// Else, if there are classes, wrap it in a span. | |
} elseif( ! empty( $classes ) ) { | |
$out = "<span $classes>$out</span>"; | |
} | |
return $out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment