【问题标题】:Remove "first" and "last" CSS classes from WooCommerce product loop从 WooCommerce 产品循环中删除“第一个”和“最后一个”CSS 类
【发布时间】:2021-08-13 17:31:29
【问题描述】:

我正在使用过滤器从产品网格中删除“first”和“last”类。

当我将 $classes 设置为 NULL 时,它会删除所有这些,但我的代码应该只删除这两个。但这不起作用。

add_filter( 'post_class', 'prefix_post_class', 21 );
function prefix_post_class( $classes ) {
    if ( 'product' == get_post_type() ) {
        $classes = array_diff( $classes, array( 'first', 'last' ) );
        // $classes = NULL; <-- This works strangely enough
    }
    return $classes;
}

【问题讨论】:

    标签: php css wordpress woocommerce product


    【解决方案1】:

    您可以改用 WooCommerce Post Class 过滤器。

    所以你得到:

    /**
     * WooCommerce Post Class filter.
     *
     * @since 3.6.2
     * @param array      $classes Array of CSS classes.
     * @param WC_Product $product Product object.
     */
    function filter_woocommerce_post_class( $classes, $product ) {
        // array_values() returns all the values from the array and indexes the array numerically.
        // array_diff - Compares array against one or more other arrays and returns the values in array that are not present in any of the other arrays.
        $classes = array_values( array_diff( $classes, array( 'first', 'last' ) ) );
        
        return $classes;
    }
    add_filter( 'woocommerce_post_class', 'filter_woocommerce_post_class', 10, 2 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 2018-12-17
      • 1970-01-01
      • 2013-11-24
      相关资源
      最近更新 更多