Woocommerce包含多个类别的面包屑

问题描述 投票:4回答:3

我在wordpress有电子商务网站。其中有很多产品和许多产品属于600 mah等多个类别移动电源是汽车,IT,媒体等。我的问题是当我去那里的产品的细节它默认只选择一个类别无论最后是否经过IT类,它都会向我展示这样的汽车/商店/工业/汽车/ 600 mah Power Bank。但我通过IT去了这个产品,所以它应该像我这样的Home / Shop / industry / IT / 600 mah Power Bank。如何从上一页来到我的路径?

php wordpress woocommerce breadcrumbs
3个回答
1
投票

如果您正在使用Woocommerce,您可以直接使用以下内容,如果不是,它将需要调整,但您明白了:

if (get_post_type() == 'product' && is_single() && ! is_attachment()) {
    echo $prepend;

    if ($terms = get_the_terms($post->ID, 'product_cat')) {
        $referer = wp_get_referer();
        foreach ($terms as $term) {
            $referer_slug = (strpos($referer, $term->slug));

            if ($referer_slug == true) {
                $category_name = $term->name;
                $ancestors = get_ancestors($term->term_id, 'product_cat');
                $ancestors = array_reverse($ancestors);

                foreach ($ancestors as $ancestor) {
                    $ancestor = get_term($ancestor, 'product_cat');

                    if (! is_wp_error($ancestor) && $ancestor) {
                        echo $before . '<a href="' . get_term_link($ancestor->slug, 'product_cat') . '">' . $ancestor->name . '</a>' . $after . $delimiter;
                    }
                }
                echo $before . '<a href="' . get_term_link($term->slug, 'product_cat') . '">' . $category_name . '</a>' . $after . $delimiter;
            }
        }
    }

    echo $before . get_the_title() . $after;
}

这里的主要工作是由wp_get_referer完成的,它获取了访问者导航到的产品的引用URL。其余代码检查URL中是否包含有效类别,并在痕迹中使用它。

有关更多信息,请参阅Jonathon Js在这里发布http://www.cryoutcreations.eu/forums/t/wrong-breadcrumbs-displayed


0
投票

我的解决方案是:

if(!empty($ breadcrumb)){

echo $wrap_before;

if (is_single() && get_post_type() == 'product') {
    $breadcrumb_diff = [];
    $breadcrumb_diff[] = $breadcrumb[0];
    if ($terms = get_the_terms($post->ID, 'product_cat')) {
        $referer = wp_get_referer();
        $site_url = site_url();
        $referer = str_replace($site_url . '/zoomagazin/', '', $referer);
        $referer_array = explode('/', $referer);
        foreach ($referer_array as $term_slug) {
            $get_term_by_slug = get_term_by('slug', $term_slug, 'product_cat');
            $breadcrumb_diff[] = [$get_term_by_slug->name, get_term_link($term_slug, 'product_cat')];
        }
        $breadcrumb_diff[]= $breadcrumb[count($breadcrumb) - 1];

        foreach ($breadcrumb_diff as $key => $crumb) {

            echo $before;

            if (!empty($crumb[1]) && sizeof($breadcrumb_diff) !== $key + 1) {
                echo '<a href="' . esc_url($crumb[1]) . '">' . esc_html($crumb[0]) . '</a>';
            } else {
                echo esc_html($crumb[0]);
            }

            echo $after;

            if (sizeof($breadcrumb) !== $key + 1) {
                echo $delimiter;
            }

        }
    }
} else {


    foreach ($breadcrumb as $key => $crumb) {

        echo $before;

        if (!empty($crumb[1]) && sizeof($breadcrumb) !== $key + 1) {
            echo '<a href="' . esc_url($crumb[1]) . '">' . esc_html($crumb[0]) . '</a>';
        } else {
            echo esc_html($crumb[0]);
        }

        echo $after;

        if (sizeof($breadcrumb) !== $key + 1) {
            echo $delimiter;
        }

    }
}

echo $wrap_after;

}


0
投票

在更新版本的WooCommerce上仍然遇到此问题的任何人的更新答案,此解决方案适用于WooCommerce 3.5.4。

此代码应放在以下文件路径中(即子主题)/wp-content/themes/your-child-theme/woocommerce/global/breadcrumb.php

它将覆盖默认的WooCommerce面包屑代码。

http://pastebin.com/raw/bemM8ZNF

/**
 * Shop breadcrumb
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/global/breadcrumb.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.3.0
 * @see         woocommerce_breadcrumb()

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( $breadcrumb ) {

    echo $wrap_before;

    if ( is_single() && get_post_type() == 'product' ) {

        echo $prepend;

        if ( $terms = get_the_terms( $post->ID, 'product_cat' ) ) {

            $referer = wp_get_referer();

            $printed = array();

            foreach( $terms as $term){

                if(in_array($term->id, $printed)) continue;

                $referer_slug = (strpos($referer, '/'.$term->slug.'/'));

                if(!$referer_slug==false){

                    $printed[] = $term->id;

                    $category_name = $term->name;
                    $ancestors = get_ancestors( $term->term_id, 'product_cat' );
                    $ancestors = array_reverse( $ancestors );

                    foreach ( $ancestors as $ancestor ) {
                        $ancestor = get_term( $ancestor, 'product_cat' );

                        if ( ! is_wp_error( $ancestor ) && $ancestor )
                            echo $before . '<a href="' . get_term_link( $ancestor->slug, 'product_cat' ) . '">' . $ancestor->name . '</a>' . $after . $delimiter;
                    }

                    echo $before . '<a href="' . get_term_link( $term->slug, 'product_cat' ) . '">' . $category_name . '</a>' . $after . $delimiter;
                }
            }

        }

        echo $before . get_the_title() . $after;

    } else {

        foreach ( $breadcrumb as $key => $crumb ) {

            echo $before;

            if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
                echo '<a href="' . esc_url( $crumb[1] ) . '">' . esc_html( $crumb[0] ) . '</a>';
            } else {
                echo esc_html( $crumb[0] );
            }

            echo $after;

            if ( sizeof( $breadcrumb ) !== $key + 1 ) {
                echo $delimiter;
            }

        }
    }

    echo $wrap_after;

}

C / O:Joris Witteman

© www.soinside.com 2019 - 2024. All rights reserved.