我要实现的是cpt的显示列表,其右侧带有评级图标,我正在浏览所有帖子,但评级仅适用于第一篇帖子。
我已经尝试循环Jquery代码,因为我正在循环自定义帖子类型,但仍然无法正常工作。
显示wp cpt。
<?php
if(is_single()) {
global $post;
$issue_ids = get_post_meta( $post->ID, '_issue_ids', true );
if(!empty($issue_ids)){
//print_r(array_values($issue_ids));
//echo $post->ID;
$args = array(
'post_type' => 'political-issue',
'post__in' => $issue_ids
);
$postslist = get_posts( $args);
//print_r($postslist);
?>
<div class='issues_container'>
<?php
$num_rate = 1;
?>
<?php foreach ( $postslist as $post ) : setup_postdata( $post ); ?>
<div class="issues_list">
<p> Issue <?php echo $num_rate; ?> : </p>
<div class="title_of_issue">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div><!-- title_of_issue -->
<div class="description_of_issue">
<?php echo wp_trim_words( get_the_content(),50,'...') ?>
</div><!-- title_of_issue -->
<!-- Adding Rating System for user only -->
<div class="user_reviews">
<?php
global $wpdb;
$tablename = $wpdb->prefix . "rating_calculator";
//global $post;
$postID = get_the_id();
//echo $postID;
$frs_finalize = 1;
$ip=0;
$id=0;
$getIp = $wpdb->get_row( "SELECT * FROM $tablename WHERE post_id = $postID and frs_finalize=$frs_finalize" );
if(!empty($getIp)){
$ip = $getIp->frs_ipaddr;
$id = $getIp->post_id;
$limit = $getIp->frs_now;
}
if(get_client_ip_env() == $ip && $postID == $id ){
echo "<div class='row_rwe finalized'><div class='rating_".$num_rate."' style='pointer-events: none;' title='Already Rated Post.' alt='Already Rated Post.' data-rate-value=".$limit."><p id='message'>".get_option('review_finalize_message')."</div></div>";
}else {
echo "<div class='row_rwe'><div class='rating_".$num_rate."'></div></div>";
}
echo "<form method='post' action=''>
<input type='hidden' id='post_id' value='".$postID."'/>
<input type='hidden' id='ipaddress' value='".get_client_ip_env()."'/>
</form>";
$getarows = $wpdb->get_results( "SELECT * FROM $tablename WHERE post_id = $postID" );
$num = count( $getarows );
if(!empty($getarows)){
$woke = 0;
foreach ( $getarows as $getarow )
{
$woke += $getarow->rating_count;
}
//echo $woke;
$total = number_format($woke/$num,0);
echo "<span id='woke'> ".$total."% Woke </span>";
if($total>0 && $total <=25){
echo "<img src='".get_option('eye-0to25')."' class='img-eye-0to25' > ";
}
if($total>26 && $total<=75){
echo "<img src='".get_option('eye-26to75')."' class='img-eye-26to75' > ";
}
if($total>76 && $total<=100){
echo "<img src='".get_option('eye-76to100')."' class='img-eye-76to100' > ";
}
}else {
echo "<span id='woke'> 0% Woke </span>";
echo "<img src='".get_option('eye-0to25')."' class='img-eye-0to25 percentageIm' > ";
}
echo "<div id='finalize'><form method='post' action=''><input type='hidden' value='1' id='idtrue'> <input type='button' id='submitFinalReview' value='Submit'/> </div>";
echo"<style> #finalize input[type='button']{background-color:".get_option('frs_color_picker')."; font-size:".get_option('frs_button_finalize_text_size')."px;}
</style> ";
?>
</div><!-- user_reviews -->
</div><!-- issues_list -->
<?php
//$num++;
$num_rate++;
endforeach;
wp_reset_postdata();
?>
</div><!-- issues_container -->
<?php
}
} ?>
jQuery代码
for (var j = 0; j < 10; j++) {
jQuery(".rating_" + j).on("change", function (ev, data) {
jQuery('.im2').removeClass("actionImage");
var post_id = jQuery("#post_id").val();
var ipaddress = jQuery("#ipaddress").val();
var limit = (data.to);
//console.log(limit);
var stars = jQuery('.rating_1' + j + ' .rate-base-layer .im2');
var num = 0;
var i;
for (i = 0; i < limit; i++) {
jQuery(stars[i]).addClass("actionImage");
//jQuery('.rating_1').css("pointer-events","none");
num++;
}
var wokeVal = num * 20;
var data = {
'action': 'frs_calculation_of_woke',
'woke_value': wokeVal,
'post_id': post_id,
'ipaddress': ipaddress,
'frs_now': limit
};
// We can also pass the url value separately from ajaxurl for front end AJAX implementations
jQuery.post(ajax_object.ajax_url, data, function (response) {
jQuery('#woke').html(response.message);
jQuery('#submitFinalReview').show();
仅适用于第一篇文章的jQuery代码。