如何将PHP订购参数添加到URL?

时间:2013-05-23 16:01:12

标签: php wordpress

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

  • 最新评论
  • 最老的评论
  • 评分最高
  • 评分最低

会相应地改变评论的顺序。

我知道链接应该是类似

的内容
  • www.example.com?orderby=comment_date&order=ASC
  • www.example.com?orderby=comment_date&order=DESC
  • www.example.com?orderby=comment_rating&order=ASC
  • www.example.com?orderby=comment_rating&order=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;
}

由于

3 个答案:

答案 0 :(得分:0)

尝试:

    <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>

答案 1 :(得分:0)

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

<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>

当您为链接调用它们时,两个参数“comment_date”和“order”的值都在$ _GET全局变量中。

答案 2 :(得分:0)

正如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 vars。我不确定你是否需要在这里逃避任何事情。 Wordpress可以自动处理。不过不要接受我的话。

相关问题