如何添加php排序参数的URL


How to add php ordering parameters to the URL?

我正在使用一个插件,支持wordpress的评论评级,我希望能够在帖子上有4个链接;

    最新评论
  • 古老评论
  • 最高额定
  • 最低额定

,它将相应地改变注释的顺序。

我知道链接应该像

这样
  • www.example.com ? orderby = comment_date&订单= ASC
  • www.example.com ? orderby = comment_date&订单= DESC
  • www.example.com ? orderby = comment_rating&订单= ASC
  • www.example.com ? orderby = comment_rating&订单= DESC

问题是,当涉及到php时,我是一个完全的新手,所以我想知道我必须在这里更改/添加什么;

<ol class="commentlist">
<?php if (function_exists(ckrating_get_comments))
{$post_id = $post->ID;$mycomments=ckrating_get_comments("post_id=$post_id&status=approve&orderby=comment_date&order=ASC");}
else$mycomments = null;wp_list_comments(array(), $mycomments);?>
</ol>

才能使上述工作?或者我需要在这里修改一些东西;

function ckrating_get_comments( $args = '' ) {
global $wpdb;
$defaults = array('status' => '', 'orderby' => 'comment_date', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
// $args can be whatever, only use the args defined in defaults to compute the key
$key = md5( serialize( compact(array_keys($defaults)) )  );
$last_changed = wp_cache_get('last_changed', 'comment');
if ( !$last_changed ) {
    $last_changed = time();
    wp_cache_set('last_changed', $last_changed, 'comment');
}
$cache_key = "get_comments:$key:$last_changed";
if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
    return $cache;
}
$post_id = absint($post_id);
if ( 'hold' == $status )
    $approved = "comment_approved = '0'";
elseif ( 'approve' == $status )
    $approved = "comment_approved = '1'";
elseif ( 'spam' == $status )
    $approved = "comment_approved = 'spam'";
else
    $approved = "( comment_approved = '0' OR comment_approved = '1' )";
$order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
    $orderby = (isset($orderby)) ? $orderby : 'comment_rating';  
$number = absint($number);
$offset = absint($offset);
if ( !empty($number) ) {
    if ( $offset )
        $number = 'LIMIT ' . $offset . ',' . $number;
    else
        $number = 'LIMIT ' . $number;
} else {
    $number = '';
}
if ( ! empty($post_id) )
    $post_where = $wpdb->prepare( 'comment_post_ID = %d AND', $post_id );
else
    $post_where = '';
$comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
wp_cache_add( $cache_key, $comments, 'comment' );
return $comments;
}

谢谢

尝试:

    <ol class="commentlist">
<?php if (function_exists(ckrating_get_comments))
{
$order_by = mysql_real_escape_string((isset($_GET['orderby']) ? $_GET['orderby'] : 'comment_date' ));
$order = mysql_real_escape_string((isset($_GET['order']) ? $_GET['order'] : 'ASC'));
$post_id = $post->ID;$mycomments=ckrating_get_comments("post_id=$post_id&status=approve&orderby=" . $order_by . "order=" . $order);}
else$mycomments = null;wp_list_comments(array(), $mycomments);?>
</ol>

为了实现你想要做的是修改第一个代码

<ol class="commentlist">
<?php if (function_exists(ckrating_get_comments))
{$post_id = $post->ID;$mycomments=ckrating_get_comments("post_id=$post_id&status=approve&orderby=".(isset($_GET['comment_date']) ? $_GET['comment_date'] ? 'comment_date')."&order=".(isset($_GET['order']) ? $_GET['order'] ? 'ASC'));}
else$mycomments = null;wp_list_comments(array(), $mycomments);?>
</ol>

当您为link调用这些参数时,两个参数"comment_date"answers"order"的值在$_GET全局变量中。

正如Petro所提到的,您的代码没有提供操作您所要求的链接的方法,但这可能足够简单,您可以添加。

要实现查询,修改如下:

"post_id=$post_id&status=approve&orderby=comment_date&order=ASC"

:

"post_id=$post_id&status=approve&orderby=" . isset($_GET['orderby']) ? $_GET['orderby'] : 'comment_date' . "&order=" . isset($_GET['order']) ? $_GET['order'] : 'ASC';

允许你传递get变量。我不确定你是否需要逃离这里。Wordpress可能会自动处理。不过别相信我的话。