想在我的自定义存档模板上有一个下拉菜单的功能,就像这样https://facetwp.com/demo/cars/?_sort=title_asc这是我的代码,但没有排序的任何自定义帖子无论是标题或评级。
<?php
$order = "&order=ASC";
if ($_POST['select'] == 'rmp_get_avg_rating') { $order =
"&order=ASC&orderby=ratings"; }
if ($_POST['select'] == 'title') { $order =
"&order=ASC&orderby=title"; }
?>
<form method="post" id="order">
Sort by:
<select name="select" onchange='this.form.submit()'>
<option value="ratings"<?php selected(
$_POST['select'],'rmp_get_avg_rating', 1 ); ?>>Highest Rated</option>
<option value="alphabetical"<?php selected(
$_POST['select'],'alphabetical' , 1 ); ?>>Alphabetical</option>
</select>
</form>
<?php $loop = new WP_Query(
array(
'post_type' => 'company',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
好的,我设法解决了这个问题下面是工作代码
<?php
$loop = isset($_GET['orderby']) ? sanitize_text_field($_GET['orderby']) :
"";
if(!empty($loop) && "title" == $loop):
$loop = new WP_Query(array(
'post_type'=>'company',
'posts_per_page'=>-1,
'orderby'=> "title",
'order'=>'ASC',
));
endif;
if(!empty($loop) && "rmp_get_avg_rating" == $loop):
$loop = new WP_Query(array(
'post_type'=>'company',
'posts_per_page'=>-1,
'orderby'=> "rmp_get_avg_rating",
'order'=>'ASC',
));
endif;
?>
<form method="GET">
<select name="orderby" id="orderby">
<option value="select">Select</option>
<option value="rmp_get_avg_rating">Highest Rated</option>
<option value="title">Alphabetical</option>
</select>
<button type="submit">Apply</button>
</form>
</div>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>