Create breadcrumb html file

Home » Snippets » Create breadcrumb html file

Created with:




Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project
// wp-plugin.php

function wpturbo_generate_breadcrumb() {
    $breadcrumb = '<div class="breadcrumb">';
    $breadcrumb .= '<a href="' . home_url() . '">Home</a>';
    if (is_category()) {
        $breadcrumb .= '<span class="delimiter"> / </span>';
        $breadcrumb .= single_cat_title('', false);
    } elseif (is_single()) {
        $breadcrumb .= '<span class="delimiter"> / </span>';
        $breadcrumb .= '<a href="' . get_permalink(get_option('page_for_posts')) . '">' . get_the_title(get_option('page_for_posts')) . '</a>';
        $breadcrumb .= '<span class="delimiter"> / </span>';
        $breadcrumb .= get_the_title();
    } elseif (is_page()) {
        $ancestors = get_post_ancestors(get_the_ID());
        if ($ancestors) {
            $ancestors = array_reverse($ancestors);
            foreach ($ancestors as $ancestor) {
                $breadcrumb .= '<span class="delimiter"> / </span>';
                $breadcrumb .= '<a href="' . get_permalink($ancestor) . '">' . get_the_title($ancestor) . '</a>';
        $breadcrumb .= '<span class="delimiter"> / </span>';
        $breadcrumb .= get_the_title();
    } elseif (is_search()) {
        $breadcrumb .= '<span class="delimiter"> / </span>';
        $breadcrumb .= 'Search results for: ' . get_search_query();
    } elseif (is_404()) {
        $breadcrumb .= '<span class="delimiter"> / </span>';
        $breadcrumb .= '404 Not Found';
    $breadcrumb .= '</div>';
    return $breadcrumb;


The code above defines a function called wpturbo_generate_breadcrumb() that generates a breadcrumb HTML based on the current page type in WordPress. It checks the current page using conditional statements (is_category(), is_single(), is_page(), is_search(), is_404()), and constructs the breadcrumb accordingly.

For each page type, the function appends the appropriate HTML markup to the $breadcrumb variable. It uses WordPress functions like home_url(), single_cat_title(), get_permalink(), get_the_title(), and get_search_query() to dynamically generate the URLs and titles.

The function returns the final breadcrumb HTML markup, which can be echoed or used wherever you want to display the breadcrumb navigation.

To use this function, you can call it in your theme or plugin file like this:

Make sure to include the necessary CSS styles for the breadcrumb in your theme or plugin stylesheet for proper styling.

Register an account to save your snippets or go Pro to get more features.