簡體   English   中英

如何將php訂購參數添加到URL?

[英]How to add php ordering parameters to the 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時,我是一個完全新手,所以我想知道我必須在此處更改/添加什么;

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

當您調用這些鏈接時,兩個參數“ comment_date”和“ 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 vars。 我不確定您是否需要在此處進行任何操作。 WordPress可能會自動處理。 不過不要相信我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM