Php Woocommerce面包屑多个类别
我有电子商务网站在wordpress。它有很多产品&许多产品属于多个类别,比如600毫安时的电力银行属于汽车,它,媒体等。我的问题是,当我进入产品的细节时,默认情况下,它只会选择一个类别,无论最后是否通过它,它都会向我显示这样的汽车:家庭/商店/工业/汽车/600毫安时电力银行。但我是通过它看这个产品的,所以它应该向我展示像这样的家庭/商店/行业/IT/600毫安时电力银行。Php Woocommerce面包屑多个类别,php,wordpress,woocommerce,breadcrumbs,Php,Wordpress,Woocommerce,Breadcrumbs,我有电子商务网站在wordpress。它有很多产品&许多产品属于多个类别,比如600毫安时的电力银行属于汽车,它,媒体等。我的问题是,当我进入产品的细节时,默认情况下,它只会选择一个类别,无论最后是否通过它,它都会向我显示这样的汽车:家庭/商店/工业/汽车/600毫安时电力银行。但我是通过它看这个产品的,所以它应该向我展示像这样的家庭/商店/行业/IT/600毫安时电力银行。 iget如何才能找到我上一页的来源?如果您使用Woocommerce,您可以直接使用以下内容,如果不使用,则需要进行调整
iget如何才能找到我上一页的来源?如果您使用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;
}
if(get_post_type()='product'&&is_single()&&&!is_attachment()){
echo$prepend;
如果($terms=get_the_terms($post->ID,'product_cat')){
$referer=wp_get_referer();
foreach($terms作为$term){
$referer_slug=(strpos($referer,$term->slug));
如果($referer\u slug==true){
$category_name=$term->name;
$祖先=获取祖先($term->term_id,'product_cat');
$ANCENTERS=数组\反向($ANCENTERS);
foreach($祖先作为$祖先){
$ANCENTOR=get_术语($ANCENTOR,'product_cat');
如果(!is_wp_error($祖先)&&$祖先){
回显$before.''.$after.$delimiter;
}
}
回显$before.''.$after.$delimiter;
}
}
}
echo$before.get_the_title().$after;
}
这里的大部分工作是由wp\u get\u referer
完成的,它获取访问者导航到的产品的参考URL。代码的其余部分检查URL中是否包含有效类别,并在面包屑中使用它
更多信息请参见Jonathon Js帖子我的解决方案是:
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;
}
if(!empty($breadcrumb)){
回音$wrap_;
if(is_single()&&get_post_type()='product'){
$breadcrumb_diff=[];
$breadcrumb_diff[]=$breadcrumb[0];
如果($terms=get_the_terms($post->ID,'product_cat')){
$referer=wp_get_referer();
$site_url=site_url();
$referer=str_replace($site_url.'/zoomagazin/',''$referer);
$referer_数组=分解('/',$referer);
foreach($referer\u数组作为$term\u 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[计数($breadcrumb)-1];
foreach($breadcrumb_diff as$key=>$crump){
回音$before;
如果(!empty($crump[1])&&sizeof($breadcrumb_diff)!=$key+1){
回声';
}否则{
echo esc_html($crumb[0]);
}
echo$after;
if(sizeof($breadcrumb)!=$key+1){
echo$分隔符;
}
}
}
}否则{
foreach($key=>$crumb的面包屑){
回音$before;
如果(!empty($crump[1])&&sizeof($breadcrumb)!==$key+1){
回声';
}否则{
echo esc_html($crumb[0]);
}
echo$after;
if(sizeof($breadcrumb)!=$key+1){
echo$分隔符;
}
}
}
回音$wrap_后;
}
更新答案对于在更新版本的WooCommerce上仍遇到此问题的任何人,此解决方案在WooCommerce 3.5.4上对我有效
此代码应放在以下文件路径中(即子主题)
/wp content/themes/your child-theme/woocommerce/global/breadcrumb.php
它将覆盖默认的WooCommerce面包屑代码
/**
*商店面包屑
*
*通过将该模板复制到yourtheme/woocommerce/global/breadcrumb.php,可以覆盖该模板。
*
*但是,WooCommerce有时需要更新模板文件,而您
*(主题开发人员)将需要将新文件复制到您的主题中
*保持兼容性。我们尽量少做这件事,但确实如此
*发生。当这种情况发生时,模板文件的版本将被碰撞和删除
*自述文件将列出任何重要的更改。
*
*@见https://docs.woocommerce.com/document/template-structure/
*@author-WooThemes
*@package/Templates
*@version 2.3.0
*@see woocommerce_breadcrumb()
如果(!已定义('ABSPATH')){
出口
}
如果($breadcrumb){
回音$wrap_;
if(is_single()&&get_post_type()='product'){
echo$prepend;
如果($terms=get_the_terms($post->ID,'product_cat')){
$referer=wp_get_referer();
$printed=array();
foreach($terms作为$term){
如果(在数组中($term->id,$printed))继续;
$referer_slug=(strpos($referer'/'.$term->slug'/');
如果(!$referer\u slug==false){
$printed[]=$term->id;
$category_name=$term->name;
$祖先=获取祖先($term->term_id,'product_cat');
$ANCENTERS=数组\反向($ANCENTERS);
foreach($祖先作为$祖先){
$ANCENTOR=get_术语($ANCENTOR,'product_cat');
如果(!is_wp_error($祖先)&&$祖先)
回显$before.''.$after.$delimiter;
}
回显$before.''.$after.$delimiter;
}
}
}
echo$before.get_the_title().$after;
}否则
/**
* 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;
}
function breadcumbs_referred_or_primary ($main, $terms)
{
// Our primary term is 520 (hardcoded)
$referer = wp_get_referer();
$referredTerm = -1;
$referredTermIndex = -1;
$primaryTermId = 520; // hardcoded
$primaryTermIndex = -1;
foreach($terms as $key => $term) {
if ($referredTerm != -1) break; // we found it in a previous iteration!
$ancestors = get_ancestors( $term->term_id, 'product_cat');
array_unshift($ancestors, $term->term_id);
if ($primaryTermIndex == -1 && in_array($primaryTermId, $ancestors)) $primaryTermIndex = $key;
foreach ($ancestors as $ancestor) {
if($referredTerm != -1) break 2; // we found it in a previous iteration!
$ancestor = get_term( $ancestor, 'product_cat' );
$referer_slug = (strpos($referer, '/'.$ancestor->slug.'/'));
if (!$referer_slug==false) { // it's found in the current level
$referredTerm = $term->term_id;
$referredTermIndex = $key;
}
}
}
// we return either the browsed terms or the primary term
if ($referredTermIndex != -1) {
return $terms[$referredTermIndex];
} else {
return $terms[$primaryTermIndex];
}
}
add_filter('woocommerce_breadcrumb_main_term', 'breadcumbs_referred_or_primary', 10, 2);