在下面的代码中,如何插入我创建的名为$website_url
的自定义变量?我想为get_highest_score
和get_highest_score_range
函数插入它。现在,输出格式如下:<li><a href="post-url">Post Name</a></li>
我想将 post-url 更改为自定义网站网址。
// Display Widget
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', esc_attr($instance['title']));
$type = esc_attr($instance['type']);
$mode = esc_attr($instance['mode']);
$limit = intval($instance['limit']);
$min_votes = intval($instance['min_votes']);
$chars = intval($instance['chars']);
$cat_ids = explode(',', esc_attr($instance['cat_ids']));
$time_range = esc_attr($instance['time_range']);
echo $before_widget.$before_title.$title.$after_title;
echo '<ul>'."\n";
switch($type) {
case 'most_rated':
get_most_rated($mode, $min_votes, $limit, $chars);
break;
case 'most_rated_category':
get_most_rated($cat_ids, $mode, $min_votes, $limit, $chars);
break;
case 'most_rated_range':
get_most_rated_range($time_range, $mode, $limit, $chars);
break;
case 'most_rated_range_category':
get_most_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);
break;
case 'highest_rated':
get_highest_rated($mode, $min_votes, $limit, $chars);
break;
case 'highest_rated_category':
get_highest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);
break;
case 'highest_rated_range':
get_highest_rated_range($time_range, $mode, $limit, $chars);
break;
case 'highest_rated_range_category':
get_highest_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);
break;
case 'lowest_rated':
get_lowest_rated($mode, $min_votes, $limit, $chars);
break;
case 'lowest_rated_category':
get_lowest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);
break;
case 'lowest_rated_range':
get_lowest_rated_range($time_range, $mode, $limit, $chars);
break;
case 'highest_score':
get_highest_score($mode, $min_votes, $limit, $chars);
break;
case 'highest_score_category':
get_highest_score_category($cat_ids, $mode, $min_votes, $limit, $chars);
break;
case 'highest_score_range':
get_highest_score_range($time_range, $mode, $limit, $chars);
break;
case 'highest_score_range_category':
get_highest_score_range_category($time_range, $cat_ids, $mode, $limit, $chars);
break;
}
echo '</ul>'."\n";
echo $after_widget;
}
我必须先在任何地方添加吗?
$custom = get_post_custom($post->ID);
$website_url = $custom["website_url"][0];
CNC中 这是我改变它的方式。我做错了,但因为网站网址没有输出。
// Display Widget
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', esc_attr($instance['title']));
$type = esc_attr($instance['type']);
$mode = esc_attr($instance['mode']);
$limit = intval($instance['limit']);
$min_votes = intval($instance['min_votes']);
$chars = intval($instance['chars']);
$cat_ids = explode(',', esc_attr($instance['cat_ids']));
$time_range = esc_attr($instance['time_range']);
$custom = get_post_custom($post->ID);
$website_url = $custom["website_url"][0];
echo $before_widget.$before_title.$title.$after_title;
echo '<ul>'."\n";
switch($type) {
case 'most_rated':
get_most_rated($mode, $min_votes, $limit, $chars);
break;
case 'most_rated_category':
get_most_rated($cat_ids, $mode, $min_votes, $limit, $chars);
break;
case 'most_rated_range':
get_most_rated_range($time_range, $mode, $limit, $chars);
break;
case 'most_rated_range_category':
get_most_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);
break;
case 'highest_rated':
get_highest_rated($mode, $min_votes, $limit, $chars);
break;
case 'highest_rated_category':
get_highest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);
break;
case 'highest_rated_range':
get_highest_rated_range($time_range, $mode, $limit, $chars);
break;
case 'highest_rated_range_category':
get_highest_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);
break;
case 'lowest_rated':
get_lowest_rated($mode, $min_votes, $limit, $chars);
break;
case 'lowest_rated_category':
get_lowest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);
break;
case 'lowest_rated_range':
get_lowest_rated_range($time_range, $mode, $limit, $chars);
break;
case 'highest_score':
get_highest_score($mode, $min_votes, $limit, $chars, $website_url);
break;
case 'highest_score_category':
get_highest_score_category($cat_ids, $mode, $min_votes, $limit, $chars);
break;
case 'highest_score_range':
get_highest_score_range($time_range, $mode, $limit, $chars, $website_url);
break;
case 'highest_score_range_category':
get_highest_score_range_category($time_range, $cat_ids, $mode, $limit, $chars);
break;
}
echo '</ul>'."\n";
echo $after_widget;
}
以下是get_highest_score
和get_highest_score_range
的功能。
### Function: Display Highest Score Page/Post
if(!function_exists('get_highest_score')) {
function get_highest_score($mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
global $wpdb;
$output = '';
if(!empty($mode) && $mode != 'both') {
$where = "$wpdb->posts.post_type = '$mode'";
} else {
$where = '1=1';
}
$temp = stripslashes(get_option('postratings_template_mostrated'));
$highest_score = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts LEFT JOIN $wpdb->postmeta AS t1 ON t1.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->postmeta As t2 ON t1.post_id = t2.post_id LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND t2.meta_value >= $min_votes AND $where ORDER BY ratings_score DESC, ratings_average DESC LIMIT $limit");
if($highest_score) {
foreach ($highest_score as $post) {
$output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
}
} else {
$output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
}
if($display) {
echo $output;
} else {
return $output;
}
}
}
...和get_highest_score_range
...
### Function: Display Highest Score Page/Post With Time Range
if(!function_exists('get_highest_score_range')) {
function get_highest_score_range($time = '1 day', $mode = '', $limit = 10, $chars = 0, $display = true) {
global $wpdb;
$min_time = strtotime('-'.$time, current_time('timestamp'));
$output = '';
if(!empty($mode) && $mode != 'both') {
$where = "$wpdb->posts.post_type = '$mode'";
} else {
$where = '1=1';
}
$temp = stripslashes(get_option('postratings_template_mostrated'));
$highest_score = $wpdb->get_results("SELECT COUNT($wpdb->ratings.rating_postid) AS ratings_users, SUM($wpdb->ratings.rating_rating) AS ratings_score, ROUND(((SUM($wpdb->ratings.rating_rating)/COUNT($wpdb->ratings.rating_postid))), 2) AS ratings_average, $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->ratings ON $wpdb->ratings.rating_postid = $wpdb->posts.ID WHERE rating_timestamp >= $min_time AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND $where GROUP BY $wpdb->ratings.rating_postid ORDER BY ratings_score DESC, ratings_average DESC LIMIT $limit");
if($highest_score) {
foreach ($highest_score as $post) {
$output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";
}
} else {
$output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
}
if($display) {
echo $output;
} else {
return $output;
}
}
}
这是expand_ratings_template
函数。
### Function: Replaces the template's variables with appropriate values
function expand_ratings_template($template, $post_id, $post_ratings_data = null, $max_post_title_chars = 0) {
global $post;
$temp_post = $post;
// Get global variables
$ratings_image = get_option('postratings_image');
$ratings_max = intval(get_option('postratings_max'));
$ratings_custom = intval(get_option('postratings_customrating'));
// Get post related variables
if(is_null($post_ratings_data)) {
$post_ratings_data = get_post_custom($post_id);
$post_ratings_users = intval($post_ratings_data['ratings_users'][0]);
$post_ratings_score = intval($post_ratings_data['ratings_score'][0]);
$post_ratings_average = floatval($post_ratings_data['ratings_average'][0]);
} else {
$post_ratings_users = intval($post_ratings_data->ratings_users);
$post_ratings_score = intval($post_ratings_data->ratings_score);
$post_ratings_average = floatval($post_ratings_data->ratings_average);
}
if($post_ratings_score == 0 || $post_ratings_users == 0) {
$post_ratings = 0;
$post_ratings_average = 0;
$post_ratings_percentage = 0;
} else {
$post_ratings = round($post_ratings_average, 1);
$post_ratings_percentage = round((($post_ratings_score/$post_ratings_users)/$ratings_max) * 100, 2);
}
$post_ratings_text = '<span class="post-ratings-text" id="ratings_'.$post_id.'_text"></span>';
// Get the image's alt text
if($ratings_custom && $ratings_max == 2) {
if($post_ratings_score > 0) {
$post_ratings_score = '+'.$post_ratings_score;
}
$post_ratings_alt_text = sprintf(_n('%s rating', '%s rating', $post_ratings_score, 'wp-postratings'), number_format_i18n($post_ratings_score)).__(',', 'wp-postratings').' '.sprintf(_n('%s vote', '%s votes', $post_ratings_users, 'wp-postratings'), number_format_i18n($post_ratings_users));
} else {
$post_ratings_score = number_format_i18n($post_ratings_score);
$post_ratings_alt_text = sprintf(_n('%s vote', '%s votes', $post_ratings_users, 'wp-postratings'), number_format_i18n($post_ratings_users)).__(',', 'wp-postratings').' '.__('average', 'wp-postratings').': '.number_format_i18n($post_ratings_average, 2).' '.__('out of', 'wp-postratings').' '.number_format_i18n($ratings_max);
}
// Check for half star
$insert_half = 0;
$average_diff = abs(floor($post_ratings_average)-$post_ratings);
if($average_diff >= 0.25 && $average_diff <= 0.75) {
$insert_half = ceil($post_ratings_average);
} elseif($average_diff > 0.75) {
$insert_half = ceil($post_ratings);
}
// Replace the variables
$value = $template;
if (strpos($template, '%RATINGS_IMAGES%') !== false) {
$post_ratings_images = get_ratings_images($ratings_custom, $ratings_max, $post_ratings, $ratings_image, $post_ratings_alt_text, $insert_half);
$value = str_replace("%RATINGS_IMAGES%", $post_ratings_images, $value);
}
if (strpos($template, '%RATINGS_IMAGES_VOTE%') !== false) {
$ratings_texts = get_option('postratings_ratingstext');
$post_ratings_images = get_ratings_images_vote($post_id, $ratings_custom, $ratings_max, $post_ratings, $ratings_image, $post_ratings_alt_text, $insert_half, $ratings_texts);
$value = str_replace("%RATINGS_IMAGES_VOTE%", $post_ratings_images, $value);
}
$value = str_replace("%RATINGS_ALT_TEXT%", $post_ratings_alt_text, $value);
$value = str_replace("%RATINGS_TEXT%", $post_ratings_text, $value);
$value = str_replace("%RATINGS_MAX%", number_format_i18n($ratings_max), $value);
$value = str_replace("%RATINGS_SCORE%", $post_ratings_score, $value);
$value = str_replace("%RATINGS_AVERAGE%", number_format_i18n($post_ratings_average, 2), $value);
$value = str_replace("%RATINGS_PERCENTAGE%", number_format_i18n($post_ratings_percentage, 2), $value);
$value = str_replace("%RATINGS_USERS%", number_format_i18n($post_ratings_users), $value);
if (strpos($template, '%POST_URL%') !== false) {
$post_link = get_permalink($post_id);
$value = str_replace("%POST_URL%", $post_link, $value);
}
if (strpos($template, '%POST_TITLE%') !== false) {
$post_title = get_the_title($post_id);
if ($max_post_title_chars > 0) {
$post_title = snippet_text($post_title, $max_post_title_chars);
}
$value = str_replace("%POST_TITLE%", $post_title, $value);
}
if (strpos($template, '%POST_EXCERPT%') !== false) {
if ($post->ID != $post_id) {
$post = &get_post($post_id);
}
$post_excerpt = ratings_post_excerpt($post_id, $post->post_excerpt, $post->post_content, $post->post_password);
$value = str_replace("%POST_EXCERPT%", $post_excerpt, $value);
}
if (strpos($template, '%POST_CONTENT%') !== false) {
if ($post->ID != $post_id) {
$post = &get_post($post_id);
}
$value = str_replace("%POST_CONTENT%", get_the_content(), $value);
}
// Return value
$post = $temp_post;
return apply_filters('expand_ratings_template', htmlspecialchars_decode($value));
}
以下是两个完整的文件(未经编辑):
答案 0 :(得分:1)
毕竟你不必使用$ website_url。这是您要编辑的expand_ratings_template函数的一部分。
if (strpos($template, '%POST_URL%') !== false) {
$post_link = get_permalink($post_id);
$value = str_replace("%POST_URL%", $post_link, $value);
}
应该成为:
if (strpos($template, '%POST_URL%') !== false) {
$custom = get_post_custom($post_id);
$post_link = $custom["website_url"][0];
$value = str_replace("%POST_URL%", $post_link, $value);
}
希望这有效。 如果没有,请插入
print_r($custom);
行后
$custom = get_post_custom($post_id);
并在此处发布内容!
答案 1 :(得分:0)
您几乎可以在任何地方插入它 - 您想要实现的目标是什么?
您想将$website_url
作为参数发送给这两个函数吗?