- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在下面的代码中,如何插入我创建的自定义变量 $website_url
?我想为 get_highest_score
插入它和 get_highest_score_range
职能。现在,输出的格式是这样的:<li><a href="post-url">Post Name</a></li>
我想将 post-url 更改为自定义网站 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];
-编辑-这是我改变它的方式。我做错了什么,因为网站 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']);
$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));
}
这是完整的两个文件(未经编辑):
最佳答案
毕竟您不必使用 $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);
并在这里发布内容!
关于php - 如何将此变量添加到此代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6593287/
我尝试理解[c代码 -> 汇编]代码 void node::Check( data & _data1, vector& _data2) { -> push ebp -> mov ebp,esp ->
我需要在当前表单(代码)的上下文中运行文本文件中的代码。其中一项要求是让代码创建新控件并将其添加到当前窗体。 例如,在Form1.cs中: using System.Windows.Forms; ..
我有此 C++ 代码并将其转换为 C# (.net Framework 4) 代码。有没有人给我一些关于 malloc、free 和 sprintf 方法的提示? int monate = ee; d
我的网络服务器代码有问题 #include #include #include #include #include #include #include int
给定以下 html 代码,将列表中的第三个元素(即“美丽”一词)以斜体显示的 CSS 代码是什么?当然,我可以给这个元素一个 id 或一个 class,但 html 代码必须保持不变。谢谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我试图制作一个宏来避免重复代码和注释。 我试过这个: #define GrowOnPage(any Page, any Component) Component.Width := Page.Surfa
我正在尝试将我的旧 C++ 代码“翻译”成头条新闻所暗示的 C# 代码。问题是我是 C# 中的新手,并不是所有的东西都像 C++ 中那样。在 C++ 中这些解决方案运行良好,但在 C# 中只是不能。我
在 Windows 10 上工作,R 语言的格式化程序似乎没有在 Visual Studio Code 中完成它的工作。我试过R support for Visual Studio Code和 R-T
我正在处理一些报告(计数),我必须获取不同参数的计数。非常简单但乏味。 一个参数的示例查询: qCountsEmployee = ( "select count(*) from %s wher
最近几天我尝试从 d00m 调试网络错误。我开始用尽想法/线索,我希望其他 SO 用户拥有可能有用的宝贵经验。我希望能够提供所有相关信息,但我个人无法控制服务器环境。 整个事情始于用户注意到我们应用程
我有一个 app.js 文件,其中包含如下 dojo amd 模式代码: require(["dojo/dom", ..], function(dom){ dom.byId('someId').i
我对“-gencode”语句中的“code=sm_X”选项有点困惑。 一个例子:NVCC 编译器选项有什么作用 -gencode arch=compute_13,code=sm_13 嵌入库中? 只有
我为我的表格使用 X-editable 框架。 但是我有一些问题。 $(document).ready(function() { $('.access').editable({
我一直在通过本教程学习 flask/python http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-wo
我想将 Vim 和 EMACS 用于 CNC、G 代码和 M 代码。 Vim 或 EMACS 是否有任何语法或模式来处理这种类型的代码? 最佳答案 一些快速搜索使我找到了 this vim 和 thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve this
这个问题在这里已经有了答案: Enabling markdown highlighting in Vim (5 个回答) 6年前关闭。 当我在 Vim 中编辑包含 Markdown 代码的 READM
我正在 Swift3 iOS 中开发视频应用程序。基本上我必须将视频 Assets 和音频与淡入淡出效果合并为一个并将其保存到 iPhone 画廊。为此,我使用以下方法: private func d
pipeline { agent any stages { stage('Build') { steps { e
我是一名优秀的程序员,十分优秀!