gpt4 book ai didi

php - 如何从 WooCommerce 产品页面上的面包屑中删除产品类别

转载 作者:行者123 更新时间:2023-12-04 08:01:08 24 4
gpt4 key购买 nike

如果有更好的方法可以做到这一点,请原谅我,因为我对这段代码不是很熟悉。我只想在面包屑上显示主页和当前产品的链接。
想要的结果 :
enter image description here
当前 :
enter image description here
我找到了面包屑的代码,有没有办法只显示第一个和最后一个面包屑而不管层次结构?

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

echo $before;

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

echo $after;

// Single product or Active title
if(is_product() || flatsome_option('wc_category_page_title')){
$key = $key+1;
if ( sizeof( $breadcrumb ) > $key) {
echo ' <span class="divider">'.$delimiter.'</span> ';
}
} else{

// Category pages
if ( sizeof( $breadcrumb ) !== $key + 1 ) {
echo ' <span class="divider">'.$delimiter.'</span> ';
}
}

}
我这样做的原因是一些产品有多个类别,默认情况下,它只会显示主要类别的面包屑。我宁愿按照所有者的建议制作一个截断版本。
我还想知道我是否可以简单地动态检索产品标题和链接 + 静态主页链接,将其制作成短代码,以便我可以将其粘贴到产品页面中。

最佳答案

如果您想从产品面包屑中删除类别和子类别 在产品页面上,您可以使用 woocommerce_get_breadcrumb 钩。

// change the breadcrumb on the product page
add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2 );
function custom_breadcrumb( $crumbs, $breadcrumb ) {

// only on the single product page
if ( ! is_product() ) {
return $crumbs;
}

// gets the first element of the array "$crumbs"
$new_crumbs[] = reset( $crumbs );
// gets the last element of the array "$crumbs"
$new_crumbs[] = end( $crumbs );

return $new_crumbs;

}
该代码已经过测试并且可以正常工作。将它添加到您的事件主题的functions.php。
一个不错的选择是 为每个产品设置主要产品类别 .您可以通过安装 Yoast SEO 来做到这一点。插入。
您可以使用 _yoast_wpseo_primary_product_cat product meta 设置主要产品类别的 id。

After setting the primary category id by editing the product in thebackend or importing a .csv file you will only need to change thepermalink and breadcrumbs based on the primary product category.


更新产品永久链接 :
// update the product permalink based on the primary product category
add_filter( 'wc_product_post_type_link_product_cat', 'change_product_permalink_by_cat', 10, 3 );
function change_product_permalink_by_cat( $term, $terms, $post ) {

// get the primary term as saved by Yoast
$primary_cat_id = get_post_meta( $post->ID, '_yoast_wpseo_primary_product_cat', true );

// if there is a primary and it's not currently chosen as primary
if ( $primary_cat_id && $term->term_id != $primary_cat_id ) {
// find the primary term in the term list
foreach ( $terms as $term_key => $term_object ) {
if ( $term_object->term_id == $primary_cat_id ) {
// return this as the primary term
$term = $terms[ $term_key ];
break;
}
}
}

return $term;
}
更新产品面包屑 在产品页面上:
// change the breadcrumb on the product page
add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2 );
function custom_breadcrumb( $crumbs, $breadcrumb ) {

// only on the single product page
if ( ! is_product() ) {
return $crumbs;
}

global $product;
$new_crumbs = array();

if ( $product->get_meta( '_yoast_wpseo_primary_product_cat', true ) ) {
// gets the first element of the array "$crumbs"
$new_crumbs[] = reset( $crumbs );
// gets the id of the primary product category
$primary_cat_id = $product->get_meta( '_yoast_wpseo_primary_product_cat', true );
// create an array with all parent categories (based on the id of the primary product category)
$parent_categories = get_ancestors( $primary_cat_id, 'product_cat' );
$parent_categories = array_reverse($parent_categories);
$parent_categories[] = $primary_cat_id;
// for each product category it gets the name and the permalink
foreach ( $parent_categories as $cat_id ) {
$term = get_term_by( 'id', $cat_id, 'product_cat' );
$new_crumbs[] = array(
0 => $term->name,
1 => esc_url( get_term_link( $term, 'product_cat' ) )
);
}
// gets the last element of the array "$crumbs"
$new_crumbs[] = end( $crumbs );
} else {
// gets the first element of the array "$crumbs"
$new_crumbs[] = reset( $crumbs );
// gets the last element of the array "$crumbs"
$new_crumbs[] = end( $crumbs );
}

return $new_crumbs;

}
该代码已经过测试并且可以正常工作。将它添加到您的事件主题的functions.php。

关于php - 如何从 WooCommerce 产品页面上的面包屑中删除产品类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66461448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com