<?php
// Function to display breadcrumb
function custom_breadcrumb() {
    echo '<div class="breadcrumb">';
    echo '<a href="' . home_url() . '" class="me-2 text-warning">Home </a>';

    if (is_category()) {
        // For category archive
        $category = get_queried_object();
        echo ' &raquo; ' . esc_html($category->name);
    } elseif (is_tag()) {
        // For tag archive
        $tag = get_queried_object();
        echo ' &raquo; Tag: ' . esc_html($tag->name);
    } elseif (is_author()) {
        // For author archive
        $author = get_queried_object();
        echo ' &raquo; Author: ' . esc_html($author->display_name);
    } elseif (is_search()) {
        // For search results
        echo ' &raquo; Search results for "' . esc_html(get_search_query()) . '"';
    } elseif (is_404()) {
        // For 404 page
        echo ' &raquo; 404 Page Not Found';
    } elseif (is_single()) {
        // For single posts
        $categories = get_the_category();
        if ($categories) {
            $category = $categories[0];
            echo ' &raquo; <a href="' . esc_url(get_category_link($category->term_id)) . '">' . esc_html($category->name) . '</a>';
        }
        echo ' &raquo; ' . esc_html(get_the_title());
    } elseif (is_page() && !is_front_page()) {
        // For pages (not homepage)
        $post = get_post();
        if ($post->post_parent) {
            // If the page has a parent, show the parent hierarchy
            $parent_id = $post->post_parent;
            $breadcrumb = [];
            while ($parent_id) {
                $page = get_post($parent_id);
                $breadcrumb[] = '<a href="' . get_permalink($page->ID) . '">' . esc_html($page->post_title) . '</a>';
                $parent_id = $page->post_parent;
            }
            echo ' &raquo; ' . implode(' &raquo; ', array_reverse($breadcrumb));
        }
        echo ' &raquo; ' . esc_html(get_the_title());
    } elseif (is_post_type_archive()) {
        // For custom post type archives
        $post_type = get_post_type_object(get_post_type());
        echo ' &raquo; ' . esc_html($post_type->labels->singular_name);
    } elseif (is_tax()) {
        // For custom taxonomy archives
        $term = get_queried_object();
        echo ' &raquo; ' . esc_html($term->name);
    } elseif (is_archive()) {
        // For date or other generic archives
        echo ' &raquo; Archive';
    }

    echo '</div>';
}
