在下面的代码中,如何插入我创建的自定义变量 $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/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has