Javier Rodriguez

Menu

inc-breadcrumbs.php

FRAMEWORK 2024-05-31

<?php   
/**
 * functions/inc-breadcrumbs.php
 * 
 * Breadcrumbs for Blog, Pages, WC account Pages
 * 
 * @author JJROD Framework
 * @see https://docs.jjrod.com
 * @version 1.0
 */

function get_breadcrumb() {
    $shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );


    echo '<a href="'.$shop_page_url.'" rel="nofollow">Catalog</a>';

    if (is_category() || is_single()) {
        echo " &nbsp&sol;  ";
        the_category(' • ');

        if (is_single()) {
            echo "  &nbsp&sol;   ";
            $categories = get_the_category();
            $category = $categories[0];
            echo '<a href="' . get_category_link($category) . '">' . $category->name . '</a>';
            echo "  &nbsp&sol;   ";
            the_title();
        }
    } elseif (is_account_page()) {
        global $wp;

        echo " &nbsp&sol;  ";
        echo '<a href="' . home_url('/my-account/') . '">My Account</a>';

        // Check if it's an endpoint and add it to the breadcrumb
        $current_endpoint = WC()->query->get_current_endpoint();
        if (!empty($current_endpoint)) {
            echo "  &nbsp&sol;  ";
            echo ucwords($current_endpoint);
        }
    }
    elseif (is_page()) {
        echo " &nbsp&sol;  ";

        global $post;
        if ($post->post_parent) {
            $parent_page = get_post($post->post_parent);
            echo '<a href="' . get_permalink($parent_page) . '">' . get_the_title($parent_page) . '</a>';
            echo "  &nbsp&sol;  ";
        }

        echo the_title();
    } elseif (is_search()) {
        echo " &nbsp&sol;  Search Results for... ";
        echo '"<em>';
        echo the_search_query();
        echo '</em>"';
    } elseif (is_account_page()) {
        global $wp;

        echo " &nbsp&sol;  ";
        echo '<a href="' . home_url('/my-account/') . '">My Account</a>';

        // Check if it's an endpoint and add it to the breadcrumb
        $current_endpoint = WC()->query->get_current_endpoint();
        if (!empty($current_endpoint)) {
            echo "  &nbsp&sol;  ";
            echo ucwords($current_endpoint);
        }
    }
}


/**************** END ****************
** END Generate breadcrumbs
**************** END ***************/
PHP