如何将 php 排序参数添加到 URL? [英] How to add php ordering parameters to the URL?

查看:30
本文介绍了如何将 php 排序参数添加到 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个插件来启用 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 时,我是一个完整的新手,所以我想知道我必须在这里更改/添加什么;

    <?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 = '' ) {全球 $wpdb;$defaults = array('status' => '', 'orderby' => 'comment_date', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0);$args = wp_parse_args( $args, $defaults );提取($args,EXTR_SKIP);//$args 可以是任何值,只使用 defaults 中定义的 args 来计算 key$key = md5(serialize(compact(array_keys($defaults))));$last_changed = wp_cache_get('last_changed', 'comment');如果(!$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, '评论' ) ) {返回 $cache;}$post_id = absint($post_id);if ('hold' == $status )$approved = "comment_approved = '0'";elseif ( '批准' == $status )$approved = "comment_approved = '1'";elseif ( '垃圾邮件' == $status )$approved = "comment_approved = '垃圾邮件'";别的$approved = "( comment_approved = '0' OR comment_approved = '1' )";$order = ( 'ASC' == $order ) ?'升序':'降序';$orderby = (isset($orderby)) ?$orderby : 'comment_rating';$number = absint($number);$offset = absint($offset);如果 ( !empty($number) ) {如果( $offset )$number = '限制' .$offset .','.$数字;别的$number = '限制' .$数字;} 别的 {$数字 = '';}如果(!空($post_id))$post_where = $wpdb->prepare('comment_post_ID = %d AND', $post_id);别的$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' );返回 $comments;}

谢谢

解决方案

正如 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['订单'] : 'ASC';

这将允许您传递 get vars.我不确定你是否需要在这里逃避任何事情.Wordpress 可能会自动处理.不过不要相信我的话.

I'm using a plugin that enables comment rating for wordpress and I want to be able to have 4 links on the post;

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

<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

解决方案

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.

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. I'm not sure if you need to escape anything here. Wordpress may handle that automatically. Don't take my word for it though.

这篇关于如何将 php 排序参数添加到 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆