Wie füge ich der URL PHP-Bestellparameter hinzu?

Lesezeit: 4 Minuten

Ich verwende ein Plugin, das die Bewertung von Kommentaren für WordPress ermöglicht, und ich möchte in der Lage sein, 4 Links für den Beitrag zu haben.

  • neuster Kommentar
  • ältester Kommentar
  • am besten bewertet
  • am niedrigsten bewertet

wodurch sich die Reihenfolge der Kommentare entsprechend ändert.

Ich weiß, dass die Links so etwas lesen sollten

  • 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

Die Sache ist, wenn es um PHP geht, bin ich ein absoluter Neuling, also habe ich mich gefragt, was ich hier ändern/hinzufügen muss;

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

damit das obige funktioniert? Oder muss ich hier etwas ändern;

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

Danke

  • Ich sehe in dem von Ihnen geposteten Code nichts, was mit dem Erstellen von Links zu tun hat. aber andererseits habe ich auch keine ahnung von wordpress

    – Petros Mastrantonas

    23. Mai 2013 um 16:04 Uhr

Versuchen:

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

  • Obwohl dieses Ome der Arbeit “näher” ist, ist es die einzige Antwort, bei der die Seite nicht unterbrochen wird. Das einzige Problem ist, dass die Kommentare nicht geladen werden

    – Paul Braun

    24. Mai 2013 um 7:29 Uhr

Um zu erreichen, was Sie zu tun versuchen, ändern Sie den ersten 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>

Wenn Sie diese für link aufrufen, stehen die Werte der beiden Parameter “comment_date” und “order” in der globalen Variable $_GET.

Wie Petro erwähnt hat, bietet Ihr Code keine Möglichkeit, die von Ihnen angeforderten Links zu manipulieren, aber das kann für Sie einfach genug sein, um ihn hinzuzufügen.

Um die Abfrage zu implementieren, ändern Sie dies:

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

dazu:

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

Dadurch können Sie get vars übergeben. Ich bin mir nicht sicher, ob Sie hier irgendetwas entkommen müssen. WordPress kann das automatisch handhaben. Nehmen Sie mich aber nicht beim Wort.

994700cookie-checkWie füge ich der URL PHP-Bestellparameter hinzu?

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy