简体   繁体   English

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

[英]How to add php ordering parameters to the URL?

I'm using a plugin that enables comment rating for wordpress and I want to be able to have 4 links on the post; 我正在使用一个可为wordpress启用评论评级的插件,我希望能够在该帖子上有4个链接;

  • newest comment 最新评论
  • oldest comment 最早的评论
  • highest rated 收视率最高
  • lowest rated 最低评分

which will change the order of the comments accordingly. 这将相应地更改评论的顺序。

I know that the links should read something like 我知道链接应显示为

  • www.example.com?orderby=comment_date&order=ASC www.example.com?orderby=comment_date&order=ASC
  • www.example.com?orderby=comment_date&order=DESC www.example.com?orderby=comment_date&order=DESC
  • www.example.com?orderby=comment_rating&order=ASC www.example.com?orderby=comment_rating&order=ASC
  • www.example.com?orderby=comment_rating&order=DESC www.example.com?orderby=comment_rating&order=DESC

The thing is, when it comes to php I'm a complete novice so I was wondering what do i have to change/add here; 问题是,当涉及到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>

in order to make the above work? 为了使以上工作? Or do I need to change something in here; 还是我需要在这里进行一些更改?

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

Thanks 谢谢

Try: 尝试:

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

In order to achieve waht your trying to do is modifying the first code 为了实现这一目标,您需要修改第一个代码

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

When you call those for link, the values of the two parameters "comment_date" and "order" are in the $_GET global variable. 当您调用这些链接时,两个参数“ comment_date”和“ order”的值在$ _GET全局变量中。

As Petro mentioned, your code does not provide a way to manipulate the links you asked for, but that may be sinple enough for you to add. 正如Petro所提到的那样,您的代码没有提供操纵所需链接的方法,但是可能足以让您添加它。

To implement the query, change this: 要实现查询,请更改以下内容:

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

to this: 对此:

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

That will allow you to pass get vars. 那将允许您传递get vars。 I'm not sure if you need to escape anything here. 我不确定您是否需要在此处进行任何操作。 Wordpress may handle that automatically. WordPress可能会自动处理。 Don't take my word for it though. 不过不要相信我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM