PHP 文件存在 & Wordpress 简码
PHP file exist & Wordpress Shortcode
我正在尝试从 .txt 文件中检索停车场的数量,它在静态站点 iframe 上工作,但我想制作一个短代码并将其放在 wordpress 主题功能文件中。
出于某种原因,它没有读取数据...
function GenerateLOT($atts = array()) {
// Default Parameters
extract(shortcode_atts(array(
'id' => 'id'
), $atts));
// Create Default Park / Help
if ($id == '') {
$id = 'PARK IDs: bahnhofgarage;';
}
// Create Park Bahnhofgarage
if ($id == 'bahnhofgarage') {
$completeBahnhof = "//xxx.de/bahnhof.txt";
if(file_exists($completeBahnhof )) {
$fp=file($completeBahnhof );
$Garage = $fp[0];
$valmpl=explode(" ",$Garage);
$Bahnhof_Platz = $valmpl[0];
$Bahnhof_Tendenz = $valmpl[1];
}
$id = $Bahnhof_Platz;
}
return $id;
}
add_shortcode('parking', 'GenerateLOT');
[停车编号='bahnhofgarage']
PS: .txt 正在正常检索,如下所示:000 - //bahnhof 27.12.15 12:46:59
出于某种原因,它只显示 $park == ''
文本,而不显示根据短代码参数的停车场。
我用过这个教程:sitepoint。com/wordpress-shortcodes-tutorial/
编辑: 有 6 个停车场。
EDIT2: 在所有实例上将 park
更改为 id
问题是您无法在远程路径上有意义地使用 file_exists
。有关详细信息,请参阅 SO answer to "file_exists() returns false even if file exist (remote URL)" question。
您应该只在该路径上调用 file()
。遇到错误会returnFALSE
if ($id == 'bahnhofgarage') {
$completeBahnhof = "//xxx.de/bahnhof.txt";
$fp=file($completeBahnhof );
if ($fp !== false) {
$Garage = $fp[0];
// rest of code
附带说明,shortcode_atts()
用于为短代码属性提供默认值,而您似乎将其用作短代码属性和内部变量名称之间的某种映射。
在简码内访问远程服务器上的文件是自找麻烦。试想一下,如果此服务器过载、响应缓慢或不再可用,将会发生什么。您真的应该异步访问该文件。如果它位于您的服务器,请通过文件系统路径访问它。
我正在尝试从 .txt 文件中检索停车场的数量,它在静态站点 iframe 上工作,但我想制作一个短代码并将其放在 wordpress 主题功能文件中。
出于某种原因,它没有读取数据...
function GenerateLOT($atts = array()) {
// Default Parameters
extract(shortcode_atts(array(
'id' => 'id'
), $atts));
// Create Default Park / Help
if ($id == '') {
$id = 'PARK IDs: bahnhofgarage;';
}
// Create Park Bahnhofgarage
if ($id == 'bahnhofgarage') {
$completeBahnhof = "//xxx.de/bahnhof.txt";
if(file_exists($completeBahnhof )) {
$fp=file($completeBahnhof );
$Garage = $fp[0];
$valmpl=explode(" ",$Garage);
$Bahnhof_Platz = $valmpl[0];
$Bahnhof_Tendenz = $valmpl[1];
}
$id = $Bahnhof_Platz;
}
return $id;
}
add_shortcode('parking', 'GenerateLOT');
[停车编号='bahnhofgarage']
PS: .txt 正在正常检索,如下所示:000 - //bahnhof 27.12.15 12:46:59
出于某种原因,它只显示 $park == ''
文本,而不显示根据短代码参数的停车场。
我用过这个教程:sitepoint。com/wordpress-shortcodes-tutorial/
编辑: 有 6 个停车场。
EDIT2: 在所有实例上将 park
更改为 id
问题是您无法在远程路径上有意义地使用 file_exists
。有关详细信息,请参阅 SO answer to "file_exists() returns false even if file exist (remote URL)" question。
您应该只在该路径上调用 file()
。遇到错误会returnFALSE
if ($id == 'bahnhofgarage') {
$completeBahnhof = "//xxx.de/bahnhof.txt";
$fp=file($completeBahnhof );
if ($fp !== false) {
$Garage = $fp[0];
// rest of code
附带说明,shortcode_atts()
用于为短代码属性提供默认值,而您似乎将其用作短代码属性和内部变量名称之间的某种映射。
在简码内访问远程服务器上的文件是自找麻烦。试想一下,如果此服务器过载、响应缓慢或不再可用,将会发生什么。您真的应该异步访问该文件。如果它位于您的服务器,请通过文件系统路径访问它。