Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Breadcrumbs
- *
- * Prints a list with breadcrumbs.
- *
- * @param array $attributes shortcode parameters
- * @return html
- * @author keha76
- */
- function keha76_breadcrumbs( $attributes = array() ) {
- // Access Globals
- global $author,$post, $wp_query;
- // Extract shortcode attributes
- extract( shortcode_atts( array(
- 'container_class' => 'breadcrumbs',
- 'container_id' => '',
- 'current_before' => "\n\t<li class=\"breadcrumbs-current\">",
- 'current_after' => '</li>',
- 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s'."\n".'</ul>'
- ), $attributes ) );
- // Variables
- $html = '';
- $home_text = apply_filters( 'keha76_breadcrumbs_home_text', __( 'Home', 'keha76' ) );
- $home_url = home_url();
- $posts_page_title = get_the_title( get_option( 'page_for_posts', true ) );
- $posts_page_permalink = get_permalink( get_option( 'page_for_posts', true ) );
- // Conditional Terms
- if ( is_home() && !is_front_page() && !is_paged() ) {
- // We are on the static posts page.
- $html .= "\n\t<li><a href=\"" . $home_url . "\">" . $home_text . "</a></li>";
- $html .= "\n\t<li class=\"breadcrumbs-current\">". $posts_page_title . "</li>";
- } elseif ( is_front_page() || is_home() ) {
- if ( is_paged() ) {
- // We are on the front home page.
- $html .= "\n\t<li><a href=\"" . $home_url . "\">" . $home_text . "</a></li>";
- } else {
- // We are on the front home page.
- $html .= "\n\t<li>" . $home_text . "</li>";
- }
- } elseif ( !is_home() && !is_front_page() || is_paged() ) {
- // Current page is paged and not front page or home.
- $html .= "\n\t<li><a href=\"" . $home_url . "\">" . $home_text . "</a></li>";
- if ( is_category() ) {
- $category = get_category( get_query_var( 'cat' ), false );
- if ( $category->parent != 0 ) {
- $category_parents = get_category_parents( $category->parent, true, ',' );
- $categories = split(',', $category_parents );
- foreach ( $categories as $cat ) {
- if ( $cat != '' ) {
- $html .= $current_before;
- $html .= $cat;
- $html .= $current_after;
- }
- }
- }
- $html .= $current_before;
- $html .= single_cat_title( '', false );
- $html .= $current_after;
- } elseif ( is_tax() ) {
- $parent = get_term_by( 'id', $wp_query->queried_object->term_id, $wp_query->queried_object->taxonomy );
- if ( $parent->parent != 0 ) {
- while ( $parent->parent != 0 ) {
- $term_id = $parent->parent;
- $parent = get_term_by( 'id', $term_id, $wp_query->queried_object->taxonomy );
- $link = get_term_link( $parent );
- $output[] = "\n\t".'<li><a href="' . $link . '">' . $parent->name .'</a></li>';
- }
- $output_reverse = array_reverse( $output );
- foreach ( $output_reverse as $list_item ) {
- $html .= $list_item;
- }
- }
- $html .= "\n\t".'<li>' . $wp_query->queried_object->name .'</li>';
- } elseif ( is_day() ) {
- $html .= "\n\t".'<li><a href="' . get_year_link( get_the_time( 'Y' ) ) . '">' . get_the_time( 'Y' ) . '</a></li>';
- $html .= "\n\t".'<li><a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '">' . get_the_time( 'F' ) . '</a></li>';
- $html .= $current_before;
- get_the_time( 'd' );
- $html .= $current_after;
- } elseif ( is_month() ) {
- $html .= "\n\t".'<li><a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a></li>';
- $html .= $current_before;
- get_the_time( 'F' );
- $html .= $current_after;
- } elseif ( is_year() ) {
- $html .= $current_before;
- get_the_time( 'Y' );
- $html .= $current_after;
- } elseif ( is_single() && !is_attachment() ) {
- $html .= $current_before;
- $html .= get_the_title();
- $html .= $current_after;
- } elseif ( is_attachment() ) {
- $post_parent = get_post( $post->post_parent );
- $post_title = trim( $parent->post_title );
- $html .= "\n\t".'<li><a href="' . get_permalink( $post_parent ) . '">' . $post_title . '</a></li>';
- $html .= $current_before;
- get_the_time( 'd' );
- $html .= $current_after;
- } elseif ( is_page() && !$post->post_parent ) {
- $html .= $current_before;
- $html .= get_the_title();
- $html .= $current_after;
- } elseif ( is_page() && $post->post_parent ) {
- $post_parent = $post->post_parent;
- $post_childs = array();
- while ( $post_parent ) {
- $page = get_page( $post_parent );
- $post_childs[] = "\n\t".'<li><a href="' . get_permalink( $page->ID) . '">' . get_the_title( $page->ID ) . '</a></li>';
- $post_parent = $page->post_parent;
- }
- $post_childs = array_reverse( $post_childs );
- foreach ( $post_childs as $post_child ) {
- $html .= $post_child;
- }
- $html .= $current_before;
- $html .= get_the_title();
- $html .= $current_after;
- } elseif ( is_search() ) {
- $search_text = __( 'Search results for <strong>%1$s</strong>', 'keha76-blank' );
- $html .= $current_before;
- $html .= printf( $search_text, get_search_query() );
- $html .= $current_after;
- } elseif ( is_tag() ) {
- $html .= $current_before;
- $html .= single_tag_title( '', false );
- $html .= $current_after;
- } elseif ( is_author() ) {
- $userdata = get_userdata( $author );
- $html .= $current_before;
- $html .= __( 'Error 404', 'keha76-blank' );
- $html .= $current_after;
- } elseif ( is_404() ) {
- $html .= $current_before;
- $html .= __( 'Error 404', 'keha76-blank' );
- $html .= $current_after;
- }
- }
- // If current page is paged
- if ( get_query_var( 'paged' ) ) {
- if ( is_home() ) {
- $html .= "\n\t<li class=\"breadcrumbs-current\"><a href=\"$posts_page_permalink\">". $posts_page_title . "</a></li>";
- }
- $html .= "\n\t<li class=\"breadcrumbs-paged\">";
- $html .= 'Page ' . get_query_var( 'paged' ) . ' of ' . $wp_query->max_num_pages;
- $html .= $current_after;
- }
- $output = sprintf( $items_wrap, $container_id, $container_class, $html );
- return $output;
- }
- add_shortcode( 'breadcrumbs', 'keha76_breadcrumbs' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement