Remove trail from Woocommerce product search resul

2019-09-02 16:27发布

问题:

To be more specific then in my previous question, I would like to achieve the following:

I am using Woocoomerce and have deleted the main shop page as I only have product categories and only need these pages. All bread crumbs are displaying correctly without the "shop" trail in them (since I have deleted that page) EXCEPT for the product search result page (the page that is displayed for results from the Woocommerce product search widget). This page shows the following breadcrumb:

"Home/Product/Search results for..."

This is the ONLY Woo page that still displays the trail "product" in it (an this is default behaviour, I have tested in on a fresh installment with twentyseventeen) and I need to remove it. Interestingly when the main shop page is not deleted, the product search results page breadcrumbs look like this:

"Home/Shop/Search results for..."

So my goal is really to have the breadcrumbs on the search results page like this

"Home/Search results for..."

Thanks!

回答1:

It's a bit of a workaround but why don't you use css:

.search-results.woocommerce .woocommerce-breadcrumb a {
  display: none;
}
.search-results.woocommerce .woocommerce-breadcrumb a:first-child {
  display: inline;
}


回答2:

I had the same problem, I overrode WooCommerce's breadcrumb.phpby copying it to mytheme/woocommerce/global/breadcrumb.php, ran a is_search() to determine whether I'm on the search results or not and then array_splice the element.

<?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;

}

you can also do

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