最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to add woocommerce products header title in woocommerce_breadcrumb hook to function.php

programmeradmin3浏览0评论

Iam new to WooCommerce theming and get stuck with category display page here is the code i have:

/**
 * Change several of the breadcrumb defaults
 */
add_filter('woocommerce_breadcrumb_defaults', 'jk_woocommerce_breadcrumbs');
function jk_woocommerce_breadcrumbs()
{
    if (is_product_category()) {
        global $wp_query;

        // get the query object
        $cat = $wp_query->get_queried_object();

        // get the thumbnail id using the queried category term_id
        $thumbnail_id = get_term_meta($cat->term_id, 'banner', true);

        // get the image URL
        $image = wp_get_attachment_url($thumbnail_id);

        // print the IMG HTML
        echo "";
        return array(
            'delimiter'   => ' / ',
            'wrap_before' => '<div class="c-main-banner c-main-banner--inner no-overlay w-100" style="background-image: url(' . $image . '"><div class="container"><ol>',
            'wrap_after'  => '</ol></div></div>',
            'before'      => '<li>',
            'after'       => '</li>',
            'home'        => _x('Home', 'breadcrumb', 'woocommerce'),
        );
    }
}


But i need to display like this:

<div class="c-main-banner c-main-banner--inner no-overlay w-100" style="background-image: url( category banner image as background );">
    <div class="container">
        <ol class="c-breadcrumb">
                <li><a href="breadcrumb slug"> breadcrumb-name </a></li>
        </ol>
        <div class="c-banner-content d-flex align-items-start justify-content-end flex-wrap flex-column">
            <big> woocommerce-products-header-title </big>
        </div>
    </div>
</div>

Iam new to WooCommerce theming and get stuck with category display page here is the code i have:

/**
 * Change several of the breadcrumb defaults
 */
add_filter('woocommerce_breadcrumb_defaults', 'jk_woocommerce_breadcrumbs');
function jk_woocommerce_breadcrumbs()
{
    if (is_product_category()) {
        global $wp_query;

        // get the query object
        $cat = $wp_query->get_queried_object();

        // get the thumbnail id using the queried category term_id
        $thumbnail_id = get_term_meta($cat->term_id, 'banner', true);

        // get the image URL
        $image = wp_get_attachment_url($thumbnail_id);

        // print the IMG HTML
        echo "";
        return array(
            'delimiter'   => ' &#47; ',
            'wrap_before' => '<div class="c-main-banner c-main-banner--inner no-overlay w-100" style="background-image: url(' . $image . '"><div class="container"><ol>',
            'wrap_after'  => '</ol></div></div>',
            'before'      => '<li>',
            'after'       => '</li>',
            'home'        => _x('Home', 'breadcrumb', 'woocommerce'),
        );
    }
}


But i need to display like this:

<div class="c-main-banner c-main-banner--inner no-overlay w-100" style="background-image: url( category banner image as background );">
    <div class="container">
        <ol class="c-breadcrumb">
                <li><a href="breadcrumb slug"> breadcrumb-name </a></li>
        </ol>
        <div class="c-banner-content d-flex align-items-start justify-content-end flex-wrap flex-column">
            <big> woocommerce-products-header-title </big>
        </div>
    </div>
</div>

Share Improve this question asked Feb 4, 2022 at 9:18 Dragan Petrovic FSDDragan Petrovic FSD 1
Add a comment  | 

1 Answer 1

Reset to default 0

I am supprised that this question still accepts answers once woocommerce is a plugin and out of the scope of this chat. Anyhow for what I have understood you want to show the product title on the woocommerce breadcrumb, Woocommerce breadcrumb already does that by default, there is no need of any changes, just remove your code.

woocommerce_breadcrumb();

If you really want to add to you function the product tittle, you can call it like this

global $product;
echo $product->name;

However this will only works on the product page, on the shop page and like so will not show the product name, or will only show the first product name.

发布评论

评论列表(0)

  1. 暂无评论