感谢您提供的一切帮助。这几天来,我一直在努力解决这个问题,但我发现自己已经不知所措了。。。任何帮助都将不胜感激——这是我第一次尝试创建插件,我对PHP非常陌生。
我有一个自定义插件,是由Joshua David Nelson的Weather in Wordpress with DarkSky 第条和this video series 来自CodeTime。它工作得非常好,除非我尝试在一个页面上使用两次短代码(它嵌入在菜单中,并且尝试在页面上再次使用短代码会导致错误)。我试图在主页上以不同的方式显示天气信息,我想我可以使用相同的功能/快捷码。
以下是插件内容,减去坐标和API键:
function weather_station(){
$coordinates = \'XXXX\'; //coordinates are here
$api_key = \'XXXX\'; //api key is here
$api_url = \'https://api.darksky.net/forecast/\'.$api_key.\'/\'.$coordinates;
$cache_key = md5( \'remote_request|\' . $api_url );
$forecast_request = get_transient( $cache_key );
if ( false === $forecast_request ) {
$forecast_request = wp_remote_get( $api_url );
if ( is_wp_error( $forecast_request ) ) {
// Cache failures for a short time, will speed up page rendering in the event of remote failure.
set_transient( $cache_key, $forecast_request, 60 );
return false;
}
// Success, cache for a longer time.
set_transient( $cache_key, $forecast_request, 300 );
}
if ( is_wp_error( $forecast_request ) ) {
return false;
}
$body = wp_remote_retrieve_body( $forecast_request );
$forecast = json_decode( $body );
//$forecast = json_decode(file_get_contents($api_url));
$icon_currently = $forecast->currently->icon;
$temperature_currently = round( $forecast->currently->temperature );
$summary_hourly = $forecast->hourly->summary;
// Set the default timezone
date_default_timezone_set($forecast->timezone);
// Get the appropriate icon
function get_icon($icon) {
if($icon===\'clear-day\') {
$the_icon = \'<i class="fas fa-sun"></i>\';
return $the_icon;
}
elseif($icon===\'clear-night\') {
$the_icon = \'<i class="fas fa-moon-stars"></i>\';
return $the_icon;
}
elseif($icon===\'rain\') {
$the_icon = \'<i class="fas fa-cloud-showers-heavy"></i>\';
return $the_icon;
}
elseif($icon===\'snow\') {
$the_icon = \'<i class="fas fa-cloud-snow"></i>\';
return $the_icon;
}
elseif($icon===\'sleet\') {
$the_icon = \'<i class="fas fa-cloud-sleet"></i>\';
return $the_icon;
}
elseif($icon===\'wind\') {
$the_icon = \'<i class="fas fa-wind"></i>\';
return $the_icon;
}
elseif($icon===\'fog\') {
$the_icon = \'<i class="fas fa-fog"></i>\';
return $the_icon;
}
elseif($icon===\'cloudy\') {
$the_icon = \'<i class="fas fa-clouds"></i>\';
return $the_icon;
}
elseif($icon===\'partly-cloudy-day\') {
$the_icon = \'<i class="fas fa-clouds-sun"></i>\';
return $the_icon;
}
elseif($icon===\'partly-cloudy-night\') {
$the_icon = \'<i class="fas fa-clouds-moon"></i>\';
return $the_icon;
}
elseif($icon===\'hail\') {
$the_icon = \'<i class="fas fa-cloud-hail"></i>\';
return $the_icon;
}
elseif($icon===\'thunderstorm\') {
$the_icon = \'<i class="fas fa-thunderstorm"></i>\';
return $the_icon;
}
elseif($icon===\'tornado\') {
$the_icon = \'<i class="fas fa-tornado"></i>\';
return $the_icon;
}
else {
$the_icon = \'<i class="fas fa-thermometer-half"></i>\';
return $the_icon;
}
}
?>
<div class="weather-station">
<div class="weather-station-button">
<span class="weather-station-icon"><?php echo get_icon($forecast->currently->icon) ?></span>
<span class="weather-station-temperature-currently"><?php echo $temperature_currently ?>°</span>
</div>
<div class="weather-station-details">
<p class="weather-station-details-title">Forecast</p>
<?php
// Start the foreach loop to get hourly forecast
foreach($forecast->daily->data as $day):
?>
<p class="weather-station-details-temperature-range"><?php echo round($day->temperatureLow).\'°/\'.round($day->temperatureHigh).\'°\'; ?></p>
<?php
// Break because we got today\'s low/high
break;
// End the foreach loop
endforeach;
?>
<p class="weather-station-details-summary"><?php echo $summary_hourly ?></p>
<?php if (!empty($forecast->alerts)) { ?>
<ul class="weather-station-details-alerts">
<?php
// Start the foreach loop to get hourly forecast
foreach($forecast->alerts as $alert):
?>
<li><a href="<?php echo $alert->uri ?>"><?php echo $alert->title ?></a><br><span>expires <?php echo date("g:i a", $alert->expires) ?></span></li>
<?php
// End the foreach loop
endforeach;
?>
</ul>
<?php } ?>
<p class="weather-station-details-darksky"><a href="https://darksky.net/poweredby/" target="_blank">Powered by Dark Sky</a></p>
</div>
</div>
<?php }
add_shortcode(\'go_weather_station\', \'weather_station\');
我打开了wp\\u debug,它向我显示了问题所在--我只是不确定修复它的最佳方法。错误消息为:
致命错误:无法在/www/wp-content/plugins/go-weather/go-weather.php:52中重新声明get\\u icon()。php在线52
后来我读到,我不应该在函数中声明函数,这是我在第52行所做的,但我尝试将其移动到没有任何效果(插件似乎根本不起作用)。如果有任何提示,我将不胜感激;我自己也会努力解决这个问题,但如果有人愿意给我提振的话,那就太棒了。非常感谢。
最合适的回答,由SO网友:Chetan Vaghela 整理而成
正如@TomJNowell所说,您绝不能嵌套这样的函数。因此,首先,你必须将这些函数移出短代码函数,并分别粘贴到插件的文件或活动主题函数中。php文件。其次,遵循法典所说的Shortcodes, 如果短代码生成大量HTMLob_start
可用于捕获输出并将其转换为字符串。我在页面中使用了两个短代码进行了测试,效果很好https://prnt.sc/qceew0
function weather_station(){
ob_start();
$coordinates = \'XXXX\'; //coordinates are here
$api_key = \'XXXXXX\'; //api key is here
$api_url = \'https://api.darksky.net/forecast/\'.$api_key.\'/\'.$coordinates;
$cache_key = md5( \'remote_request|\' . $api_url );
$forecast_request = get_transient( $cache_key );
if ( false === $forecast_request ) {
$forecast_request = wp_remote_get( $api_url );
if ( is_wp_error( $forecast_request ) ) {
// Cache failures for a short time, will speed up page rendering in the event of remote failure.
set_transient( $cache_key, $forecast_request, 60 );
return false;
}
// Success, cache for a longer time.
set_transient( $cache_key, $forecast_request, 300 );
}
if ( is_wp_error( $forecast_request ) ) {
return false;
}
$body = wp_remote_retrieve_body( $forecast_request );
$forecast = json_decode( $body );
//$forecast = json_decode(file_get_contents($api_url));
$icon_currently = $forecast->currently->icon;
$temperature_currently = round( $forecast->currently->temperature );
$summary_hourly = $forecast->hourly->summary;
// Set the default timezone
date_default_timezone_set($forecast->timezone);
?>
<div class="weather-station">
<div class="weather-station-button">
<span class="weather-station-icon"><?php echo get_icon($forecast->currently->icon) ?></span>
<span class="weather-station-temperature-currently"><?php echo $temperature_currently ?>°</span>
</div>
<div class="weather-station-details">
<p class="weather-station-details-title">Forecast</p>
<?php
// Start the foreach loop to get hourly forecast
foreach($forecast->daily->data as $day):
?>
<p class="weather-station-details-temperature-range"><?php echo round($day->temperatureLow).\'°/\'.round($day->temperatureHigh).\'°\'; ?></p>
<?php
// Break because we got today\'s low/high
break;
// End the foreach loop
endforeach;
?>
<p class="weather-station-details-summary"><?php echo $summary_hourly ?></p>
<?php if (!empty($forecast->alerts)) { ?>
<ul class="weather-station-details-alerts">
<?php
// Start the foreach loop to get hourly forecast
foreach($forecast->alerts as $alert):
?>
<li><a href="<?php echo $alert->uri ?>"><?php echo $alert->title ?></a><br><span>expires <?php echo date("g:i a", $alert->expires) ?></span></li>
<?php
// End the foreach loop
endforeach;
?>
</ul>
<?php } ?>
<p class="weather-station-details-darksky"><a href="https://darksky.net/poweredby/" target="_blank">Powered by Dark Sky</a></p>
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode(\'go_weather_station\', \'weather_station\');
// Get the appropriate icon
function get_icon($icon) {
if($icon===\'clear-day\') {
$the_icon = \'<i class="fas fa-sun"></i>\';
return $the_icon;
}
elseif($icon===\'clear-night\') {
$the_icon = \'<i class="fas fa-moon-stars"></i>\';
return $the_icon;
}
elseif($icon===\'rain\') {
$the_icon = \'<i class="fas fa-cloud-showers-heavy"></i>\';
return $the_icon;
}
elseif($icon===\'snow\') {
$the_icon = \'<i class="fas fa-cloud-snow"></i>\';
return $the_icon;
}
elseif($icon===\'sleet\') {
$the_icon = \'<i class="fas fa-cloud-sleet"></i>\';
return $the_icon;
}
elseif($icon===\'wind\') {
$the_icon = \'<i class="fas fa-wind"></i>\';
return $the_icon;
}
elseif($icon===\'fog\') {
$the_icon = \'<i class="fas fa-fog"></i>\';
return $the_icon;
}
elseif($icon===\'cloudy\') {
$the_icon = \'<i class="fas fa-clouds"></i>\';
return $the_icon;
}
elseif($icon===\'partly-cloudy-day\') {
$the_icon = \'<i class="fas fa-clouds-sun"></i>\';
return $the_icon;
}
elseif($icon===\'partly-cloudy-night\') {
$the_icon = \'<i class="fas fa-clouds-moon"></i>\';
return $the_icon;
}
elseif($icon===\'hail\') {
$the_icon = \'<i class="fas fa-cloud-hail"></i>\';
return $the_icon;
}
elseif($icon===\'thunderstorm\') {
$the_icon = \'<i class="fas fa-thunderstorm"></i>\';
return $the_icon;
}
elseif($icon===\'tornado\') {
$the_icon = \'<i class="fas fa-tornado"></i>\';
return $the_icon;
}
else {
$the_icon = \'<i class="fas fa-thermometer-half"></i>\';
return $the_icon;
}
}
SO网友:Kaperto
您可以像这样分解“get\\u icon”函数
function get_icon($icon) {
$names = [
"clear-day" => "sun",
"clear-night" => "moon-stars",
"rain" => "cloud-showers-heavy",
"snow" => "cloud-snow",
"sleet" => "cloud-sleet",
"wind" => "wind",
"fog" => "fog",
"cloudy" => "clouds",
"partly-cloudy-day" => "clouds-sun",
"partly-cloudy-night" => "clouds-moon",
"hail" => "cloud-hail",
"thunderstorm" => "thunderstorm",
"tornado" => "tornado",
];
$value = $names[$icon] ?? "thermometer-half";
return "<i class=\\"fas fa-{$value}\\"></i>";
}