我的目标是 Select 当前的WooCommerce排序选项,例如websitename.com/?orderby=popularity
,并将当前的查询 Select 设置为body类.(理想情况下是body class="popularity"
).
在下面的示例中,我使用if/elseif语句.我试着用一个switch 操作器以及简单地使用global $orderby
.我还没有找到任何关于这个问题的例子,感觉很困,所以我很感谢任何帮助.
非常感谢.以下是我当前的文档:
add_filter( 'body_class', 'gaz_wc_sorting_as_body_class' );
function gaz_wc_sorting_as_body_class( $class ){
$orderby_value = $_GET['orderby'];
if ($orderby_value === 'popularity') {
array( ' popular ' => $class);
} elseif ($orderby_value === 'date') {
array( ' recent ' => $class);
} elseif ($orderby_value === 'price') {
array( ' cost-lo-hi ' => $class);
} elseif ($orderby_value === 'price-desc') {
array( ' cost-hi-lo ' => $class);
} else {
array( 'sort-clear ' => $class);
}
return $class;
}