教学用自定义面包屑导航
wptutor
2023-12-27
48 Views
function custom_breadcrumbs() {
// Define breadcrumb separator
$separator = ' » '; // Change this to your desired separator
// Start breadcrumb output
$output = '<div class="erx-m-bot erx-current">';
$output .= '<a href="' . esc_url(home_url('/')) . '">' . __('首页', 'text-domain') . '</a>' . $separator; // "首页" means "Home"
if (!is_front_page()) {
// Add Home link if not on the front page
$output .= '<a href="' . esc_url(home_url('/')) . '">' . __('Home', 'text-domain') . '</a>' . $separator;
}
if (is_category() || is_single()) {
// Category and single post breadcrumbs
$category = get_the_category();
if ($category) {
$output .= '<a href="' . esc_url(get_category_link($category[0]->term_id)) . '">' . $category[0]->name . '</a>';
}
if (is_single()) {
$output .= $separator . get_the_title();
}
} elseif (is_page()) {
// Page breadcrumbs
global $post;
if ($post->post_parent) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_post($parent_id);
$breadcrumbs[] = '<a href="' . esc_url(get_permalink($page->ID)) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) {
$output .= $crumb . $separator;
}
}
$output .= get_the_title();
}
// End breadcrumb output
$output .= '</div>';
// Output the breadcrumbs
echo $output;
}