当您提交表单时,表单会自动将您的选择值添加到url。没有必要创造这样的价值&orderby=date&order=dsc
. 若要在单个select值中传递多个信息,可以使用更简单的value和If语句。对于最新选项,我将使用newest
as value和add做这样一个if语句。
// Url /?s=test&order=newest
$orderby = \'\';
$order = \'\';
// Get filtered $_GET[\'order\'] parameter
$value = filter_input( INPUT_GET, \'order\', FILTER_SANITIZE_STRING );
if( $value === \'newest\' ) {
$orderby = \'date\';
$order = \'desc\';
}
// Modify query using $orderby and $order parameters
使用JavaScript为我选择选项也是个坏主意。你应该检查电流
order
参数并将选定属性添加到选项中。
// Url /?s=test&order=newest
// Get filtered $_GET[\'order\'] parameter
$value = filter_input( INPUT_GET, \'order\', FILTER_SANITIZE_STRING );
$selected = ($value === \'newest\') ? \'selected\': \'\' ;
?>
<select name="order">
<option value="newest" <?php echo esc_attr( $selected ); ?>></option>
</select>
我认为你不应该费心制作好的url。对于少量的参数来说,创建一个好的url可能很有诱惑力,但当你的应用程序越来越大时,添加额外的重写用例和逻辑是没有目的的。如果你不得不说服你的老板或客户放弃好的URL,请在搜索页面上向他们展示亚马逊。我将他们视为专家,他们在搜索过程中不会使用好的URL。
下面的代码是如何按邮件排序的完整示例date
, price
和size
. 处决wpse_288655_display_form
要在其中显示排序表单的函数。
/**
* Display sort form
*/
function wpse_288655_display_form() {
/**
* Get all params from url which are not part of our sort form
* and display it in form as hidden inputs.
*/
$search_param = filter_input( INPUT_GET, \'s\', FILTER_SANITIZE_STRING );
/**
* Current order value to select proper field
*/
$value = filter_input( INPUT_GET, \'order\', FILTER_SANITIZE_STRING );
?>
<form method="get">
<?php wpse_288655_display_select(\'order\', \'Sort by:\', wpse_288655_get_order_by_options(), $value ); ?>
<?php
/**
* Display all params from url which do not apply to our sort form
*/
?>
<input type="hidden" name="s" value="<?php esc_attr_e( $search_param ); ?>">
<button type="submit"><?php esc_html_e(\'Sort\'); ?></button>
</form>
<?php
}
/**
* Get sort options
*/
function wpse_288655_get_order_by_options(){
return array(
\'\' => \'\',
\'newest\' => __(\'Newest\'),
\'oldest\' => __(\'Oldest\'),
\'most_expensive\' => __(\'Most Expensive\'),
\'least_expensive\' => __(\'Least Expensive\'),
\'largest\' => __(\'Largest\'),
\'smallest\' => __(\'Smallest\'),
);
}
/**
* Display select field
*/
function wpse_288655_display_select( $name, $label, $options, $value = \'\' ) {
?>
<label><?php esc_html_e( $label ) ?></label>
<select name="<?php esc_attr_e( $name ) ?>">
<?php wpse_288655_display_options( $options, $value ); ?>
</select>
<?php
}
/**
* Display select options
*/
function wpse_288655_display_options( $options, $value ) {
foreach( $options as $option_value => $option_label ):
$selected = ( $option_value === $value ) ? \' selected\' : \'\';
?>
<option value="<?php esc_attr_e( $option_value ) ?>"<?php esc_attr_e( $selected ) ?>><?php esc_html_e( $option_label ) ?></option>
<?php
endforeach;
}
/**
* Sort posts using pre_get_posts filter
*/
function wpse_288655_order_posts( $query ) {
/**
* Execute query only when we are on search page and this is main query
*/
if ( $query->is_search() && $query->is_main_query() ) {
$value = filter_input( INPUT_GET, \'order\', FILTER_SANITIZE_STRING );
$order_by_options = wpse_288655_get_order_by_options();
if( isset( $order_by_options[ $value ] ) && !empty( $value ) ) {
switch( $value ) {
case \'newest\':
$query->set( \'order\', \'desc\' );
$query->set( \'orderby\', \'date\' );
break;
case \'oldest\':
$query->set( \'order\', \'asc\' );
$query->set( \'orderby\', \'date\' );
break;
case \'most_expensive\':
$query->set( \'meta_key\', \'price\' ); // Your custom meta_key
$query->set( \'order\', \'desc\' );
$query->set( \'orderby\', \'meta_value_num\' );
break;
case \'least_expensive\':
$query->set( \'meta_key\', \'price\' ); // Your custom meta_key
$query->set( \'order\', \'asc\' );
$query->set( \'orderby\', \'meta_value_num\' );
break;
case \'largest\':
$query->set( \'meta_key\', \'size\' ); // Your custom meta_key
$query->set( \'order\', \'desc\' );
$query->set( \'orderby\', \'meta_value_num\' );
break;
case \'smallest\':
$query->set( \'meta_key\', \'size\' ); // Your custom meta_key
$query->set( \'order\', \'asc\' );
$query->set( \'orderby\', \'meta_value_num\' );
break;
default:
break;
}
}
}
}
add_filter(\'pre_get_posts\', \'wpse_288655_order_posts\');