Skip to content

Instantly share code, notes, and snippets.

@psorensen
Created May 21, 2018 17:38
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 psorensen/7bf045bdfc9f71bad093765087345905 to your computer and use it in GitHub Desktop.
Save psorensen/7bf045bdfc9f71bad093765087345905 to your computer and use it in GitHub Desktop.
<?php
/**
* Functionality for modifying and outputting breadcrumbs provided by Yoast.
*
* @package IPM
*/
namespace IPM\Breadcrumbs;
/**
* Sets up this file with the WordPress API.
*
* @return void
*/
function load() {
add_action( 'after_setup_theme', __NAMESPACE__ . '\\setup' );
}
/**
* Register various methods with the WordPress API.
*
* @return void
*/
function setup() {
add_action( 'ipm_content_top', __NAMESPACE__ . '\\display_breadcrumbs', 5 );
add_filter( 'wp_head', __NAMESPACE__ . '\\maybe_hide_title' );
add_filter( 'wpseo_breadcrumb_links', __NAMESPACE__ . '\\insert_breadcrumbs' );
}
/**
* Display consistent breadcrumbs
*
* @action ipm_content_top
* @todo build_futures removes this action. need to adjust to account for namespace
* @return string
*/
function display_breadcrumbs() {
if ( is_home() && ! is_paged() ) {
return;
} else {
echo '<div class="custom-bread-crumb">';
if ( function_exists( 'yoast_breadcrumb' ) ) {
yoast_breadcrumb( '<div class="breadcrumb-trail">', '</div>' );
}
echo '</div><!-- .custom-bread-crumb -->';
}
}
/**
* Conditionally prevent output of title breadcrumb
*
* @param array $links Array of links.
*
* @return array
*/
function maybe_hide_title( $links ) {
if ( is_singular( 'post' )
|| ( function_exists( '\IPM\Special_Reports\is_special_report' ) && \IPM\Special_Reports\is_special_report() )
) { ?>
<!-- Hide last breadcrumb -->
<style>
.breadcrumb-trail .breadcrumb_last {
display: none;
}
</style><?php
}
return $links;
}
/**
* Alter the breadcrumb markup to add elements not naturally added by Yoast.
*
* @param array $links Array of link elements.
*
* @return array
*/
function insert_breadcrumbs( $links ) {
if ( is_tax( 'ipm_hot_topics' ) ) {
array_splice( $links, 1, 0, [
[
'text' => '<a href="">Hot Topics</a>',
'url' => '',
'allow_html' => true,
],
] );
}
return $links;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment