首页 > 解决方案 > 从 Woocommerce 产品搜索结果页面中删除跟踪

问题描述

更具体地说,在我之前的问题中,我想实现以下目标:

我正在使用 Woocoomerce 并删除了主商店页面,因为我只有产品类别并且只需要这些页面。除了产品搜索结果页面(Woocommerce 产品搜索小部件的结果显示页面)之外,所有面包屑都正确显示,没有“商店”踪迹(因为我已删除该页面)。此页面显示以下面包屑:

“首页/产品/搜索结果...”

这是唯一一个仍然在其中显示跟踪“产品”的 Woo 页面(这是默认行为,我已经用二十七号在新的一期中进行了测试),我需要删除它。有趣的是,当主商店页面没有被删除时,产品搜索结果页面的面包屑看起来是这样的:

“首页/商店/搜索结果...”

所以我的目标真的是像这样在搜索结果页面上有面包屑

“首页/搜索结果...”

谢谢!

标签: phpwordpresswoocommerce

解决方案


我遇到了同样的问题,我通过将 WooCommercebreadcrumb.php复制到 来覆盖它mytheme/woocommerce/global/breadcrumb.php,运行 ais_search()以确定我是否在搜索结果中,然后array_splice是元素。

<?php
/**
 * 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/
 * @package     WooCommerce/Templates
 * @version     2.3.0
 * @see         woocommerce_breadcrumb()
 */

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

if ( ! empty( $breadcrumb ) ) {

    echo $wrap_before;

    if ( is_search() ) {
        array_splice( $breadcrumb, 1, 1 ); // considering that the element is at the second position.
        // Otherwise :
        // array_splice( $breadcrumb, 0, 1 ); if it's at the first position, etc.
    }

    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;

}

你也可以

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

        // Ignore the "Product" item
        if ( $crumb[0] == 'Product' ) {
            continue;
        }

        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;
        }
    }
    ...

推荐阅读