
Michelle
Ich möchte, dass bei jedem Besuch 5 zufällige Bewertungen auf meiner Homepage erscheinen.
Ich habe einen Code gefunden, um alle Bewertungen abzurufen:
//add get product reviews to homepage
function get_woo_reviews()
{
$count = 0;
$html_r = "";
$title="";
$args = array(
'post_type' => 'product'
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach($comments as $comment) :
$title="".get_the_title( $comment->comment_post_ID ).'';
$html_r = $html_r. "" .$title."";
$html_r = $html_r. "" .$comment->comment_content."";
$html_r = $html_r."Posted By".$comment->comment_author." On ".$comment->comment_date. "";
endforeach;
return $html_r;
}
add_shortcode('woo_reviews', 'get_woo_reviews');
Und es funktioniert gut, wenn ich den Shortcode hinzufüge [woo_reviews]
dazu Testseite.
Wie ändere ich dies, um nur 5 zufällige Bewertungen zu erhalten?
Wie würde ich diese Seite jetzt auch formatieren, damit sie nur 5 Bewertungen enthält und das Erscheinungsbild der Bewertungen auf der Seite ändern kann (Abstand, Schriftart usw.)?
Fügen Sie die folgenden Codeausschnitte hinzu –
function get_woo_reviews()
{
$comments = get_comments(
array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
)
);
shuffle($comments);
$comments = array_slice( $comments, 0, 5 );
$html="<ul>";
foreach( $comments as $comment ) :
$html .= '<li><h2>'.get_the_title( $comment->comment_post_ID ).'</h2>';
$html .= '<p>' .$comment->comment_content.'</p>';
$html .= "<p>Posted By :".$comment->comment_author." On ".$comment->comment_date. "</p></li>";
endforeach;
$html .= '</ul>';
ob_start();
echo $html;
$html = ob_get_contents();
ob_end_clean();
return $html;
}
add_shortcode('woo_reviews', 'get_woo_reviews');

LoicTheAztec
Mit dem Kommentar WP_Comment_Query
, Kommentare dürfen nicht in zufälliger Reihenfolge sein. Sie müssen also stattdessen eine einfache, leichte SQL-Abfrage verwenden, indem Sie dediziertes WordPress verwendenWPDB
Klasse.
Im folgenden Code können Sie die Stile und die HTML-Struktur ändern, um die gewünschte Ausgabe zu erhalten. Sie können auch die Anzahl der Bewertungen festlegen, die Sie in zufälliger Reihenfolge anzeigen möchten, indem Sie das verfügbare Shortcode-Argument verwenden “Grenze” (Standard ist eingestellt auf 5):
add_shortcode('woo_reviews', 'get_random_woo_reviews');
function get_random_woo_reviews( $atts ){
// Shortcode Attributes
$atts = shortcode_atts( array(
'limit' => '5', // <== Set to 5 reviews by default
), $atts, 'woo_reviews' );
global $wpdb;
// The SQL random query on product reviews
$comments = $wpdb->get_results( $wpdb->prepare("
SELECT *
FROM {$wpdb->prefix}comments c
INNER JOIN {$wpdb->prefix}posts p ON c.comment_post_ID = p.ID
WHERE c.comment_type="review" AND p.post_status="publish"
ORDER BY RAND() LIMIT %d
", intval( esc_attr($atts['limit']) ) ) );
ob_start(); // Start buffering
## CSS applied styles
?>
<style>
ul.product-reviews, ul.product-reviews li { list-style: none; margin:0; padding:0; line-height: normal;}
ul.product-reviews li { display:block; max-width: 200px, padding: 10px; display:inline-block; vertical-align: text-top;}
ul.product-reviews li .title {font-size: 1.2em;}
ul.product-reviews li .content {max-width: 180px; font-size: 0.9em; margin-bottom: 6px;}
ul.product-reviews li .author, ul.product-reviews li .date {display: block; font-size: 0.75em;}
</style>
<?php
## HTML structure
?>
<ul class="product-reviews"><?php
foreach ( $comments as $comment ) {
?>
<li>
<h4 class="title"><?php echo get_the_title( $comment->comment_post_ID ); ?></h4>
<div class="content"><?php echo $comment->comment_content; ?></div>
<span class="author"><?php printf( __("Posted By %s") . ' ', '<strong>' . $comment->comment_author . '</strong>' ); ?></span>
<span class="date"><?php printf( __("On %s"), '<strong>' . date_i18n( 'l jS \of F Y', strtotime( $comment->comment_date) ) . '</strong>' ); ?></span>
</li>
<?php
}
?></ul><?php
return ob_get_clean(); // Return the buffered output
}
Der Code wird in die function.php-Datei Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt. Getestet und funktioniert.
VERWENDUNGSZWECK: [woo_reviews]
oder in php: echo do_shortcode( "[woo_reviews]" );
9164700cookie-checkWooCommerce: Zeigen Sie einige Bewertungen zufällig auf der Startseite anyes