WordPress中标签的过滤功能


Filter function for label in WordPress

我正在开发一个使用WooCommerce的WordPress网站。WooCommerce将产品属性标签中的每个单词都大写,这会影响长标签的可读性。

function wc_attribute_label( $name, $product = '' ) {
global $wpdb;
if ( taxonomy_is_product_attribute( $name ) ) {
    $name = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $name ) );
    $label = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) );
if ( ! $label ) {
    $label = ucfirst( $name );
    }
} elseif ( $product && ( $attributes = $product->get_attributes() ) && isset( $attributes[ sanitize_title( $name ) ]['name'] ) ) {
    // Attempt to get label from product, as entered by the user
    $label = $attributes[ sanitize_title( $name ) ]['name'];
} else {
    // Just format as best as we can
    $label = ucwords( str_replace( '-', ' ', $name ) );
}
return apply_filters( 'woocommerce_attribute_label', $label, $name, $product ); }

我需要将上面代码片段中的"ucwords"改为"ucfirst":

$label = ucwords( str_replace( '-', ' ', $name ) );

我想在我的functions.php文件中创建一个过滤器。看起来应该很容易,但是我有很多困难。任何帮助都将非常感激。这是我的文件:

add_filter( 'woocommerce_attribute_label' , 'custom_attribute_label' );
function custom_attribute_label( $label ) {
$label = ucfirst ( $label );
return ( $label ); }

问题是add_filter的语法。

已经纠正了代码。现在试试这个,让我知道是否有帮助。

add_filter( 'woocommerce_attribute_label' , 'custom_attribute_label',99,3 );
function custom_attribute_label( $label, $name, $product ) {
  $label = ucfirst ( $label );
  return ( $label ); 
}