Multiple categories assigned to a single product breaking the breadcrumb

custom bread crumb not working with multiple categories assigned to a single product, we do not have the categories page, so don't want to show categories in the URL, but need to show the categories navigation on the breadcrumb .

?php

function bootstrap_breadcrumb($custom_home_icon = false, $custom_post_types = false)
{
    wp_reset_query();
    global $post;
    $is_custom_post = $custom_post_types ? is_singular($custom_post_types) : false;
    $post_type      = get_post_type(get_the_ID());
    if (!is_front_page()  !is_home()) {
        echo 'ol class=d-flex breadcrumb-wrapper';
        echo 'lia href=';
        echo get_option('home');
        echo '';
        if ($custom_home_icon) {
            echo $custom_home_icon;
        } else {
            _e('Home', 'star');
        }

        echo /a/li;
        if (has_category()) {
            echo 'li class=activea href=' . esc_url(get_permalink(get_page(get_the_category($post-ID)))) . '';
            the_category(', ');
            echo '/a/li';
        }
        if (is_category() || is_single() || $is_custom_post) {
            if (is_category()) {
                echo 'li class=activea href=' . esc_url(get_permalink(get_page(get_the_category($post-ID)))) . '' . get_the_category($post-ID)[0]-name . '/a/li';
            }

            if ($is_custom_post) {
                if (is_singular('projects')) {
                    $page             = get_field('project_archive', 'option');
                    $project_category = wp_get_post_terms(get_the_ID(), 'industry', array(fields = all));
                    $industry         = '';
                    $term_id          = array();
                    if (sizeof($project_category)  0) {
                        $count = 0;
                        foreach ($project_category as $lat_project) {
                            $term_id[]   = $lat_project-term_id;
                            $custom_link = get_field('custom_url', $lat_project-taxonomy . '_' . $lat_project-term_id);
                            $term_link   = get_term_link($lat_project-slug, 'industry');
                            if ($custom_link == '') {
                                $custom_link = $term_link;
                            }
                            if ($count == 0) {
                                $industry = 'a href=' . $custom_link . '' . $lat_project-name . '/a';
                            }

                            $count++;
                        }
                    }
                    echo 'li class=activea href=' . get_permalink($page) . '' . get_post_type_object(get_post_type($post))-label . '/a/li';
                    if ($industry != '') {
                        echo 'li class=active' . $industry . '/li';
                    }
                } elseif (is_singular('products')) {
                    $page             = get_field('product_archive', 'option');
                    $product_category = wp_get_post_terms(get_the_ID(), 'product_category', array(fields = all));

                    $prod_cat = '';
                    $term_id  = array();
                    if (sizeof($product_category)  0) {
                        $count = 0;
                        foreach ($product_category as $lat_product) {
                            $term_id[]   = $lat_product-term_id;
                            $custom_link = get_field('custom_url', $lat_product-taxonomy . '_' . $lat_product-term_id);
                            //$pageSlug       = basename($custom_link);
                            $pageIdfromslug = url_to_postid($custom_link);
                            $current_post   = get_post($pageIdfromslug);
                            $postParent_id  = $current_post-post_parent;
                            $postParent     = get_post($postParent_id);
                            if ($postParent-post_parent  0) {
                                $postparent_title = $postParent-post_title;
                                $postparent_link  = get_permalink($postParent_id);
                            }
                            $term_link = get_term_link($lat_product-slug, 'product_category');

                            if ($custom_link == '') {
                                $custom_link = $term_link;
                            }
                            if ($count == 0) {
                                if ($postparent_title != ) {
                                    $prod_cat = 'a href=' . $postparent_link . '' . $postparent_title . '/a';
                                }
                                $prod_cat .= 'a href=' . $custom_link . '' . $lat_product-name . '/a';
                            }

                            $count++;
                        }
                    }
                    echo 'li class=activea href=' . get_permalink($page) . '' . get_post_type_object(get_post_type($post))-label . '/a/li';
                    if ($prod_cat != '') {
                        echo 'li class=active' . $prod_cat . '/li';
                    }
                } elseif (is_singular('resources')) {
                    $page = get_field('resource_archive', 'option');
                    echo 'li class=activea href=' . get_permalink($page) . '' . get_post_type_object(get_post_type($post))-label . '/a/li';
                } else {
                    echo 'li class=activea href=' . get_option('home') . '/' . get_post_type_object(get_post_type($post))-name . '' . get_post_type_object(get_post_type($post))-label . '/a/li';
                }
                if ($post-post_parent) {
                    $home = get_page(get_option('page_on_front'));
                    for ($i = count($post-ancestors) - 1; $i = 0; $i--) {
                        if (($home-ID) != ($post-ancestors[$i])) {
                            echo 'lia href=';
                            echo get_permalink($post-ancestors[$i]);
                            echo '';
                            echo get_the_title($post-ancestors[$i]);
                            echo /a/li;
                        }
                    }
                }
            } elseif ($post_type == 'products') {
                $page             = get_field('product_archive', 'option');
                $product_category = wp_get_post_terms(get_the_ID(), 'product_category', array(fields = all));
                $prod_cat         = '';
                $term_id          = array();
                if (sizeof($product_category)  0) {
                    $count = 0;
                    foreach ($product_category as $lat_product) {
                        $term_id[]   = $lat_product-term_id;
                        $custom_link = get_field('custom_url', $lat_product-taxonomy . '_' . $lat_product-term_id);
                        //$pageSlug       = basename($custom_link);
                        $pageIdfromslug = url_to_postid($custom_link);
                        $current_post   = get_post($pageIdfromslug);
                        $postParent_id  = $current_post-post_parent;
                        $postParent     = get_post($postParent_id);
                        if ($postParent-post_parent  0) {
                            $postparent_title = $postParent-post_title;
                            $postparent_link  = get_permalink($postParent_id);
                            $postParent_Id2   = $postParent-post_parent;
                            $postParent2      = get_post($postParent_Id2);
                            if ($postParent2-post_parent  0) {
                                $postParent_title2 = $postParent2-post_title;
                                $postparent_link2  = get_permalink($postParent_Id2);
                            }
                        }

                        $term_link = get_term_link($lat_product-slug, 'product_category');
                        if ($custom_link == '') {
                            $custom_link = $term_link;
                        }
                        if ($count == 0) {
                            if ($postParent_title2 != ) {
                                $prod_cat = 'a href=' . $postparent_link2 . '' . $postParent_title2 . '/a';
                            }
                            if ($postparent_title != ) {
                                $prod_cat .= 'a href=' . $postparent_link . '' . $postparent_title . '/a';
                            }

                            $prod_cat .= 'a href=' . $custom_link . '' . $lat_product-name . '/a';
                        }

                        $count++;
                    }
                }
                echo 'li class=activea href=' . get_permalink($page) . '' . get_post_type_object(get_post_type($post))-label . '/a/li';
                if ($prod_cat != '') {
                    echo 'li class=active' . $prod_cat . '/li';
                }
            }
            if (is_single()) {
                echo 'li class=active' . get_the_title($post-ID) . '/li';
            }

        } elseif (is_page()  $post-post_parent) {
            $home = get_page(get_option('page_on_front'));
            for ($i = count($post-ancestors) - 1; $i = 0; $i--) {
                if (($home-ID) != ($post-ancestors[$i])) {
                    echo 'lia href=';
                    echo get_permalink($post-ancestors[$i]);
                    echo '';
                    echo get_the_title($post-ancestors[$i]);
                    echo /a/li;
                }
            }
            echo 'li class=active' . get_the_title($post-ID) . '/li';
        } elseif (is_page()) {
            echo 'li class=active' . get_the_title($post-ID) . '/li';
        } elseif (is_404()) {
            echo 'li class=active404/li';
        }
        echo '/ol';
    }
}

Topic twitter-bootstrap breadcrumb categories customization custom-post-types Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.