我在函数中有以下函数。php,允许按字母顺序或按近距排序。我想把字母顺序改为按浏览量最大的排序。这是我为分拣机准备的代码,它是一个下拉选择菜单:
function sorter($wp_query) {
if(mysql_real_escape_string($_REQUEST["sorting"]) == "date"){
usort($wp_query->posts, \'cmp_date\');
}else if(mysql_real_escape_string($_REQUEST["sorting"]) == "title"){
usort($wp_query->posts, \'cmp_alpha\');
}
}
这是分拣机的代码:
function custom_sort(){
// retrieve the sorting variables
$sort_selected = mysql_real_escape_string($_REQUEST["sorting"]); ?>
<form action="" method="post">
<div class="sorting">
<span>Sorting:</span>
<select name="sorting" onchange="this.form.submit();">
<?php switch($sort_selected){
case "";
case "date";
?>
<option value="date">Recency</option>
<option value="title">Alphabetic</option>
<option value="views">Views</option>
<?php break;
case "title";?>
<option value="title">Alphabetic</option>
<option value="date">Recency</option>
<option value="views">Views</option>
<?php break;
case "views";?>
<option value="views">Views</option>
<option value="title">Alphabetic</option>
<option value="date">Recency</option>
<?php break;
} // end switch ?>
</select>
</div><!-- exit div sorting -->
</form>
<?php }
我想我需要这样做(我在这个网站的另一个问题中看到):
<?php $args = array(
\'post_status\' => \'publish\',
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\'
);
$the_query = new WP_Query($args); //http://codex.wordpress.org/Class_Reference/WP_Query
//the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
//do your magic
endwhile;
但我不明白如何将上面的代码翻译成适合我当前功能的代码。我用它来获取帖子视图:
function getPostViews($postID){
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
return "0 View";
}
return $count.\' Views\';
}
function setPostViews($postID) {
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
我希望这是有意义的,并提前表示感谢。