/** * Gets the items for the breadcrumb trail. This is the heart of the script. It checks the current page * being viewed and decided based on the information provided by WordPress what items should be * added to the breadcrumb trail. * * @since 0.4.0 * @todo Build in caching based on the queried object ID. * @param array $args Mixed arguments for the menu. * @return array List of items to be shown in the trail. */ function breadcrumb_trail_get_items($args = array()) { global $wp_rewrite; /* Get the textdomain. */ $textdomain = breadcrumb_trail_textdomain(); /* Set up an empty trail array and empty path. */ $trail = array(); $path = ''; /* If $show_home is set and we're not on the front page of the site, link to the home page. */ if (!is_front_page() && $args['show_home']) { $trail[] = '<a href="' . home_url() . '" title="' . esc_attr(get_bloginfo('name')) . '" rel="home" class="trail-begin">' . $args['show_home'] . '</a>'; } /* If viewing the front page of the site. */ if (is_front_page()) { if ($args['show_home'] && $args['front_page']) { $trail['trail_end'] = "{$args['show_home']}"; } } elseif (is_home()) { $home_page = get_page(get_queried_object_id()); $trail = array_merge($trail, breadcrumb_trail_get_parents($home_page->post_parent, '')); $trail['trail_end'] = get_the_title($home_page->ID); } elseif (is_singular()) { /* Get singular post variables needed. */ $post = get_queried_object(); $post_id = absint(get_queried_object_id()); $post_type = $post->post_type; $parent = absint($post->post_parent); /* Get the post type object. */ $post_type_object = get_post_type_object($post_type); /* If viewing a singular 'post'. */ if ('post' == $post_type) { /* If $front has been set, add it to the $path. */ $path .= trailingslashit($wp_rewrite->front); /* If there's a path, check for parents. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* Map the permalink structure tags to actual links. */ $trail = array_merge($trail, breadcrumb_trail_map_rewrite_tags($post_id, get_option('permalink_structure'), $args)); } elseif ('attachment' == $post_type) { /* If $front has been set, add it to the $path. */ $path .= trailingslashit($wp_rewrite->front); /* If there's a path, check for parents. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* Map the post (parent) permalink structure tags to actual links. */ $trail = array_merge($trail, breadcrumb_trail_map_rewrite_tags($post->post_parent, get_option('permalink_structure'), $args)); } elseif ('page' !== $post_type) { /* If $front has been set, add it to the $path. */ if ($post_type_object->rewrite['with_front'] && $wp_rewrite->front) { $path .= trailingslashit($wp_rewrite->front); } /* If there's a slug, add it to the $path. */ if (!empty($post_type_object->rewrite['slug'])) { $path .= $post_type_object->rewrite['slug']; } /* If there's a path, check for parents. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* If there's an archive page, add it to the trail. */ if (!empty($post_type_object->has_archive)) { $trail[] = '<a href="' . get_post_type_archive_link($post_type) . '" title="' . esc_attr($post_type_object->labels->name) . '">' . $post_type_object->labels->name . '</a>'; } } /* If the post type path returns nothing and there is a parent, get its parents. */ if (empty($path) && 0 !== $parent || 'attachment' == $post_type) { $trail = array_merge($trail, breadcrumb_trail_get_parents($parent, '')); } elseif (0 !== $parent && is_post_type_hierarchical($post_type)) { $trail = array_merge($trail, breadcrumb_trail_get_parents($parent, '')); } /* Display terms for specific post type taxonomy if requested. */ if (!empty($args["singular_{$post_type}_taxonomy"]) && ($terms = get_the_term_list($post_id, $args["singular_{$post_type}_taxonomy"], '', ', ', ''))) { $trail[] = $terms; } /* End with the post title. */ $post_title = get_the_title(); if (!empty($post_title)) { $trail['trail_end'] = $post_title; } } elseif (is_archive()) { /* If viewing a taxonomy term archive. */ if (is_tax() || is_category() || is_tag()) { /* Get some taxonomy and term variables. */ $term = get_queried_object(); $taxonomy = get_taxonomy($term->taxonomy); /* Get the path to the term archive. Use this to determine if a page is present with it. */ if (is_category()) { $path = get_option('category_base'); } elseif (is_tag()) { $path = get_option('tag_base'); } else { if ($taxonomy->rewrite['with_front'] && $wp_rewrite->front) { $path = trailingslashit($wp_rewrite->front); } $path .= $taxonomy->rewrite['slug']; } /* Get parent pages by path if they exist. */ if ($path) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* If the taxonomy is hierarchical, list its parent terms. */ if (is_taxonomy_hierarchical($term->taxonomy) && $term->parent) { $trail = array_merge($trail, breadcrumb_trail_get_term_parents($term->parent, $term->taxonomy)); } /* Add the term name to the trail end. */ $trail['trail_end'] = single_term_title('', false); } elseif (is_post_type_archive()) { /* Get the post type object. */ $post_type_object = get_post_type_object(get_query_var('post_type')); /* If $front has been set, add it to the $path. */ if ($post_type_object->rewrite['with_front'] && $wp_rewrite->front) { $path .= trailingslashit($wp_rewrite->front); } /* If there's a slug, add it to the $path. */ if (!empty($post_type_object->rewrite['slug'])) { $path .= $post_type_object->rewrite['slug']; } /* If there's a path, check for parents. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* Add the post type [plural] name to the trail end. */ $trail['trail_end'] = $post_type_object->labels->name; } elseif (is_author()) { /* If $front has been set, add it to $path. */ if (!empty($wp_rewrite->front)) { $path .= trailingslashit($wp_rewrite->front); } /* If an $author_base exists, add it to $path. */ if (!empty($wp_rewrite->author_base)) { $path .= $wp_rewrite->author_base; } /* If $path exists, check for parent pages. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* Add the author's display name to the trail end. */ $trail['trail_end'] = get_the_author_meta('display_name', get_query_var('author')); } elseif (is_time()) { if (get_query_var('minute') && get_query_var('hour')) { $trail['trail_end'] = get_the_time(__('g:i a', $textdomain)); } elseif (get_query_var('minute')) { $trail['trail_end'] = sprintf(__('Minute %1$s', $textdomain), get_the_time(__('i', $textdomain))); } elseif (get_query_var('hour')) { $trail['trail_end'] = get_the_time(__('g a', $textdomain)); } } elseif (is_date()) { /* If $front has been set, check for parent pages. */ if ($wp_rewrite->front) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $wp_rewrite->front)); } if (is_day()) { $trail[] = '<a href="' . get_year_link(get_the_time('Y')) . '" title="' . get_the_time(esc_attr__('Y', $textdomain)) . '">' . get_the_time(__('Y', $textdomain)) . '</a>'; $trail[] = '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '" title="' . get_the_time(esc_attr__('F', $textdomain)) . '">' . get_the_time(__('F', $textdomain)) . '</a>'; $trail['trail_end'] = get_the_time(__('d', $textdomain)); } elseif (get_query_var('w')) { $trail[] = '<a href="' . get_year_link(get_the_time('Y')) . '" title="' . get_the_time(esc_attr__('Y', $textdomain)) . '">' . get_the_time(__('Y', $textdomain)) . '</a>'; $trail['trail_end'] = sprintf(__('Week %1$s', $textdomain), get_the_time(esc_attr__('W', $textdomain))); } elseif (is_month()) { $trail[] = '<a href="' . get_year_link(get_the_time('Y')) . '" title="' . get_the_time(esc_attr__('Y', $textdomain)) . '">' . get_the_time(__('Y', $textdomain)) . '</a>'; $trail['trail_end'] = get_the_time(__('F', $textdomain)); } elseif (is_year()) { $trail['trail_end'] = get_the_time(__('Y', $textdomain)); } } } elseif (is_search()) { $trail['trail_end'] = sprintf(__('Search results for "%1$s"', $textdomain), esc_attr(get_search_query())); } elseif (is_404()) { $trail['trail_end'] = __('404 Not Found', $textdomain); } /* Allow devs to step in and filter the $trail array. */ return apply_filters('breadcrumb_trail_items', $trail, $args); }
/** * Gets the items for the breadcrumb trail. This is the heart of the script. It checks the current page * being viewed and decided based on the information provided by WordPress what items should be * added to the breadcrumb trail. * * @since 0.4.0 * @todo Build in caching based on the queried object ID. * @access public * @param array $args Mixed arguments for the menu. * @return array List of items to be shown in the trail. */ function breadcrumb_trail_get_items($args = array()) { global $wp_rewrite; /* Set up an empty trail array and empty path. */ $trail = array(); $path = ''; /* If $show_home is set and we're not on the front page of the site, link to the home page. */ if (!is_front_page() && $args['show_home']) { if (is_multisite() && true === $args['network']) { $trail[] = '<a href="' . network_home_url() . '">' . $args['show_home'] . '</a>'; $trail[] = '<a href="' . home_url() . '" title="' . esc_attr(get_bloginfo('name')) . '" rel="home" class="trail-begin">' . get_bloginfo('name') . '</a>'; } else { $trail[] = '<a href="' . home_url() . '" title="' . esc_attr(get_bloginfo('name')) . '" rel="home" class="trail-begin">' . $args['show_home'] . '</a>'; } } /* if Buddypress exists */ if (class_exists('BP_Core_user') && !bp_is_blog_page()) { $trail = array_merge($trail, breadcrumb_trail_get_buddypress_items()); } elseif (function_exists('is_bbpress') && is_bbpress()) { $trail = array_merge($trail, breadcrumb_trail_get_bbpress_items()); } elseif (is_front_page()) { if (!is_paged() && $args['show_home'] && $args['front_page']) { if (is_multisite() && true === $args['network']) { $trail[] = '<a href="' . network_home_url() . '">' . $args['show_home'] . '</a>'; $trail[] = get_bloginfo('name'); } else { $trail[] = $args['show_home']; } } elseif (is_paged() && $args['show_home'] && $args['front_page']) { if (is_multisite() && true === $args['network']) { $trail[] = '<a href="' . network_home_url() . '">' . $args['show_home'] . '</a>'; $trail[] = '<a href="' . home_url() . '" title="' . esc_attr(get_bloginfo('name')) . '" rel="home" class="trail-begin">' . get_bloginfo('name') . '</a>'; } else { $trail[] = '<a href="' . home_url() . '" title="' . esc_attr(get_bloginfo('name')) . '" rel="home" class="trail-begin">' . $args['show_home'] . '</a>'; } } } elseif (is_home()) { $home_page = get_page(get_queried_object_id()); $trail = array_merge($trail, breadcrumb_trail_get_parents($home_page->post_parent, '')); if (is_paged()) { $trail[] = '<a href="' . get_permalink($home_page->ID) . '" title="' . esc_attr(get_the_title($home_page->ID)) . '">' . get_the_title($home_page->ID) . '</a>'; } else { $trail[] = "<span>" . get_the_title($home_page->ID) . "</span>"; } } elseif (is_singular()) { /* Get singular post variables needed. */ $post = get_queried_object(); $post_id = absint(get_queried_object_id()); $post_type = $post->post_type; $parent = absint($post->post_parent); /* Get the post type object. */ $post_type_object = get_post_type_object($post_type); /* If viewing a singular 'post'. */ if ('post' == $post_type) { /* If $front has been set, add it to the $path. */ $path .= trailingslashit($wp_rewrite->front); /* If there's a path, check for parents. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* Map the permalink structure tags to actual links. */ $trail = array_merge($trail, breadcrumb_trail_map_rewrite_tags($post_id, get_option('permalink_structure'), $args)); } elseif ('attachment' == $post_type) { /* Get the parent post ID. */ $parent_id = $post->post_parent; /* If the attachment has a parent (attached to a post). */ if (0 < $parent_id) { /* Get the parent post type. */ $parent_post_type = get_post_type($parent_id); /* If the post type is 'post'. */ if ('post' == $parent_post_type) { /* If $front has been set, add it to the $path. */ $path .= trailingslashit($wp_rewrite->front); /* If there's a path, check for parents. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* Map the post (parent) permalink structure tags to actual links. */ $trail = array_merge($trail, breadcrumb_trail_map_rewrite_tags($post->post_parent, get_option('permalink_structure'), $args)); } elseif ('page' !== $parent_post_type) { $parent_post_type_object = get_post_type_object($parent_post_type); /* If $front has been set, add it to the $path. */ if ($parent_post_type_object->rewrite['with_front'] && $wp_rewrite->front) { $path .= trailingslashit($wp_rewrite->front); } /* If there's a slug, add it to the $path. */ if (!empty($parent_post_type_object->rewrite['slug'])) { $path .= $parent_post_type_object->rewrite['slug']; } /* If there's a path, check for parents. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* If there's an archive page, add it to the trail. */ if (!empty($parent_post_type_object->has_archive)) { /* Add support for a non-standard label of 'archive_title' (special use case). */ $label = !empty($parent_post_type_object->labels->archive_title) ? $parent_post_type_object->labels->archive_title : $parent_post_type_object->labels->name; $trail[] = '<a href="' . get_post_type_archive_link($parent_post_type) . '" title="' . esc_attr($label) . '">' . $label . '</a>'; } } } } elseif ('page' !== $post_type) { /* If $front has been set, add it to the $path. */ if ($post_type_object->rewrite['with_front'] && $wp_rewrite->front) { $path .= trailingslashit($wp_rewrite->front); } /* If there's a slug, add it to the $path. */ if (!empty($post_type_object->rewrite['slug'])) { $path .= $post_type_object->rewrite['slug']; } /* If there's a path, check for parents. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* If there's an archive page, add it to the trail. */ if (!empty($post_type_object->has_archive)) { /* Add support for a non-standard label of 'archive_title' (special use case). */ $label = !empty($post_type_object->labels->archive_title) ? $post_type_object->labels->archive_title : $post_type_object->labels->name; $trail[] = '<a href="' . get_post_type_archive_link($post_type) . '" title="' . esc_attr($label) . '">' . $label . '</a>'; } } /* If the post type path returns nothing and there is a parent, get its parents. */ if (empty($path) && 0 !== $parent || 'attachment' == $post_type) { $trail = array_merge($trail, breadcrumb_trail_get_parents($parent, '')); } elseif (0 !== $parent && is_post_type_hierarchical($post_type)) { $trail = array_merge($trail, breadcrumb_trail_get_parents($parent, '')); } /* Display terms for specific post type taxonomy if requested. */ if (!empty($args["singular_{$post_type}_taxonomy"]) && ($terms = get_the_term_list($post_id, $args["singular_{$post_type}_taxonomy"], '', ', ', ''))) { $trail[] = "<span>" . $terms . "</span>"; } /* End with the post title. */ $post_title = single_post_title('', false); if (1 < get_query_var('page') && !empty($post_title)) { $trail[] = '<a href="' . get_permalink($post_id) . '" title="' . esc_attr($post_title) . '">' . $post_title . '</a>'; } elseif (!empty($post_title)) { $trail[] = '<span>' . $post_title . '</span>'; } } elseif (is_archive()) { /* If viewing a taxonomy term archive. */ if (is_tax() || is_category() || is_tag()) { /* Get some taxonomy and term variables. */ $term = get_queried_object(); $taxonomy = get_taxonomy($term->taxonomy); /* Get the path to the term archive. Use this to determine if a page is present with it. */ if (is_category()) { $path = get_option('category_base'); } elseif (is_tag()) { $path = get_option('tag_base'); } else { if ($taxonomy->rewrite['with_front'] && $wp_rewrite->front) { $path = trailingslashit($wp_rewrite->front); } $path .= $taxonomy->rewrite['slug']; } /* Get parent pages by path if they exist. */ if ($path) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* Add post type archive if its 'has_archive' matches the taxonomy rewrite 'slug'. */ if ($taxonomy->rewrite['slug']) { /* Get public post types that match the rewrite slug. */ $post_types = get_post_types(array('public' => true, 'has_archive' => $taxonomy->rewrite['slug']), 'objects'); /** * If any post types are found, loop through them to find one that matches. * The reason for this is because WP doesn't match the 'has_archive' string * exactly when calling get_post_types(). I'm assuming it just matches 'true'. */ if (!empty($post_types)) { foreach ($post_types as $post_type_object) { if ($taxonomy->rewrite['slug'] === $post_type_object->has_archive) { /* Add support for a non-standard label of 'archive_title' (special use case). */ $label = !empty($post_type_object->labels->archive_title) ? $post_type_object->labels->archive_title : $post_type_object->labels->name; /* Add the post type archive link to the trail. */ $trail[] = '<a href="' . get_post_type_archive_link($post_type_object->name) . '" title="' . esc_attr($label) . '">' . $label . '</a>'; /* Break out of the loop. */ break; } } } } /* If the taxonomy is hierarchical, list its parent terms. */ if (is_taxonomy_hierarchical($term->taxonomy) && $term->parent) { $trail = array_merge($trail, breadcrumb_trail_get_term_parents($term->parent, $term->taxonomy)); } /* Add the term name to the trail end. */ if (is_paged()) { $trail[] = '<a href="' . esc_url(get_term_link($term, $term->taxonomy)) . '" title="' . esc_attr(single_term_title('', false)) . '">' . single_term_title('', false) . '</a>'; } else { $trail[] = '<span>' . single_term_title('', false) . '</span>'; } } elseif (is_post_type_archive()) { /* Get the post type object. */ $post_type_object = get_post_type_object(get_query_var('post_type')); /* If $front has been set, add it to the $path. */ if ($post_type_object->rewrite['with_front'] && $wp_rewrite->front) { $path .= trailingslashit($wp_rewrite->front); } /* If there's a slug, add it to the $path. */ if (!empty($post_type_object->rewrite['slug'])) { $path .= $post_type_object->rewrite['slug']; } /* If there's a path, check for parents. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* Add the post type [plural] name to the trail end. */ if (is_paged()) { $trail[] = '<a href="' . esc_url(get_post_type_archive_link($post_type_object->name)) . '" title="' . esc_attr(post_type_archive_title('', false)) . '">' . post_type_archive_title('', false) . '</a>'; } else { $trail[] = "<span>" . post_type_archive_title('', false) . "</span>"; } } elseif (is_author()) { /* Get the user ID. */ $user_id = get_query_var('author'); /* If $front has been set, add it to $path. */ if (!empty($wp_rewrite->front)) { $path .= trailingslashit($wp_rewrite->front); } /* If an $author_base exists, add it to $path. */ if (!empty($wp_rewrite->author_base)) { $path .= $wp_rewrite->author_base; } /* If $path exists, check for parent pages. */ if (!empty($path)) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $path)); } /* Add the author's display name to the trail end. */ if (is_paged()) { $trail[] = '<a href="' . esc_url(get_author_posts_url($user_id)) . '" title="' . esc_attr(get_the_author_meta('display_name', $user_id)) . '">' . get_the_author_meta('display_name', $user_id) . '</a>'; } else { $trail[] = "<span>" . get_the_author_meta('display_name', $user_id) . "</span>"; } } elseif (is_time()) { if (get_query_var('minute') && get_query_var('hour')) { $trail[] = get_the_time(__('g:i a', 'kleo_framework')); } elseif (get_query_var('minute')) { $trail[] = sprintf(__('Minute %1$s', 'kleo_framework'), get_the_time(__('i', 'kleo_framework'))); } elseif (get_query_var('hour')) { $trail[] = get_the_time(__('g a', 'kleo_framework')); } } elseif (is_date()) { /* If $front has been set, check for parent pages. */ if ($wp_rewrite->front) { $trail = array_merge($trail, breadcrumb_trail_get_parents('', $wp_rewrite->front)); } if (is_day()) { $trail[] = '<a href="' . get_year_link(get_the_time('Y')) . '" title="' . get_the_time(esc_attr__('Y', 'kleo_framework')) . '">' . get_the_time(__('Y', 'kleo_framework')) . '</a>'; $trail[] = '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '" title="' . get_the_time(esc_attr__('F', 'kleo_framework')) . '">' . get_the_time(__('F', 'kleo_framework')) . '</a>'; if (is_paged()) { $trail[] = '<a href="' . get_day_link(get_the_time('Y'), get_the_time('m'), get_the_time('d')) . '" title="' . get_the_time(esc_attr__('d', 'kleo_framework')) . '">' . get_the_time(__('d', 'kleo_framework')) . '</a>'; } else { $trail[] = get_the_time(__('d', 'kleo_framework')); } } elseif (get_query_var('w')) { $trail[] = '<a href="' . get_year_link(get_the_time('Y')) . '" title="' . get_the_time(esc_attr__('Y', 'kleo_framework')) . '">' . get_the_time(__('Y', 'kleo_framework')) . '</a>'; if (is_paged()) { $trail[] = get_archives_link(add_query_arg(array('m' => get_the_time('Y'), 'w' => get_the_time('W')), home_url()), sprintf(__('Week %1$s', 'kleo_framework'), get_the_time(esc_attr__('W', 'kleo_framework'))), false); } else { $trail[] = sprintf(__('Week %1$s', 'kleo_framework'), get_the_time(esc_attr__('W', 'kleo_framework'))); } } elseif (is_month()) { $trail[] = '<a href="' . get_year_link(get_the_time('Y')) . '" title="' . get_the_time(esc_attr__('Y', 'kleo_framework')) . '">' . get_the_time(__('Y', 'kleo_framework')) . '</a>'; if (is_paged()) { $trail[] = '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '" title="' . get_the_time(esc_attr__('F', 'kleo_framework')) . '">' . get_the_time(__('F', 'kleo_framework')) . '</a>'; } else { $trail[] = get_the_time(__('F', 'kleo_framework')); } } elseif (is_year()) { if (is_paged()) { $trail[] = '<a href="' . get_year_link(get_the_time('Y')) . '" title="' . esc_attr(get_the_time(__('Y', 'kleo_framework'))) . '">' . get_the_time(__('Y', 'kleo_framework')) . '</a>'; } else { $trail[] = get_the_time(__('Y', 'kleo_framework')); } } } } elseif (is_search()) { if (is_paged()) { $trail[] = '<a href="' . get_search_link() . '" title="' . sprintf(esc_attr__('Search results for "%1$s"', 'kleo_framework'), esc_attr(get_search_query())) . '">' . sprintf(__('Search results for "%1$s"', 'kleo_framework'), esc_attr(get_search_query())) . '</a>'; } else { $trail[] = "<span>" . sprintf(__('Search results for "%1$s"', 'kleo_framework'), esc_attr(get_search_query())) . "</span>"; } } elseif (is_404()) { $trail[] = "<span>" . __('404 Not Found', 'kleo_framework') . "</span>"; } /* Check for pagination. */ if (is_paged()) { $trail[] = "<span>" . sprintf(__('Page %d', 'kleo_framework'), absint(get_query_var('paged'))) . "</span>"; } elseif (is_singular() && 1 < get_query_var('page')) { $trail[] = "<span>" . sprintf(__('Page %d', 'kleo_framework'), absint(get_query_var('page'))) . "</span>"; } /* Allow devs to step in and filter the $trail array. */ return apply_filters('breadcrumb_trail_items', $trail, $args); }