facetwp_facet_display_value
Overview
Modify the display value (i.e. label) for specific facet choices.
Parameters
- $label | string | The facet choice label (text or HTML)
- $params | array | An associative array of facet settings (see below)
$params = [
'selected' => false, // TRUE or FALSE
'facet' => [
'name' => 'vehicle_type',
'label' => 'Vehicle Type',
'type' => 'radio',
'source' => 'tax/vehicle_type',
// + other facet-specific settings
],
'row' => [
'id' => 123,
'post_id' => 42,
'facet_name' => 'vehicle_type',
'facet_value' => 'suv',
'facet_display_value' => 'SUV',
// + other columns from the "facetwp_index" DB table
]
];
Usage examples
Display facet choices as images
To display a facet’s choices as icons or images, add the following code to your (child) theme’s functions.php.
The icon/image names are based on each facet choice’s facet_value
(its “raw value”), e.g. “car”, “truck”, “suv”. The facet_value
of a choice can be seen in the URL parameter of the facet when you select that choice.
add_filter( 'facetwp_facet_display_value', function( $label, $params ) {
// only apply to a facet named "vehicle_type"
if ( 'vehicle_type' == $params['facet']['name'] ) {
// get the raw value
$val = $params['row']['facet_value'];
// use the raw value to generate the image URL
$label = '<img src="/wp-content/themes/yourtheme/images/{val}.svg" alt="{val}" />';
$label = str_replace( '{val}', $val, $label );
}
return $label;
}, 10, 2 );
Translate facet choices
To translate or customize a specific facet choice (its front-end facet_display_value
), add the following code to your (child) theme’s functions.php:
add_filter( 'facetwp_facet_display_value', function( $label, $params ) {
if ( 'Monday' == $label ) { // translate "Monday" into Greek
$label = 'Δευτέρα';
}
return $label;
}, 10, 2 );