WordPress 页面模板中的 PHP 代码不执行 [英] PHP code in WordPress page template doesn't execute
问题描述
谁能告诉我为什么 wordpress '页面模板' 中的这段代码不执行?
Can anyone tell me why this code in a wordpress 'page template' doesn't execute?
'Echo' 似乎可以工作,但所有 'include' 和 'print_r()' 以及其他函数都不起作用.这个确切的代码适用于我的家庭服务器,但不适用于 wordpress 托管网站:
'Echo' seem to work but all 'include' and 'print_r()' and other functions don't. This exact code works on my home server but not on wordpress hosted site:
<?php
/**
* Template Name: fbshares
*
* A custom page template for displaying all posts.
*
* The "Template Name:" bit above allows this to be selectable
* from a dropdown menu on the edit page screen.
*
* @package WordPress
* @subpackage Twenty_Ten
* @since Twenty Ten 1.0
*/
get_header(); ?>
<div id="container">
<div id="content" role="main" class="fullpagejoel">
<?php
echo "x";
$url = "http://www.google.com";
$social = array(
'twitter' => 'http://cdn.api.twitter.com/1/urls/count.json?url=',
'facebook' => 'https://graph.facebook.com/',
'stumbleupon' => 'http://www.stumbleupon.com/services/1.01/badge.getinfo?url='
);
$json = file_get_contents($social['twitter'].$url, false);
$a = json_decode($json, true);
$r['twitter'] = $a['count'];
print_r($a);
echo count($a).' [OK]';
?>
推荐答案
我刚刚发现了问题.逐项调试后,问题是WordPress不允许您运行file_get_contents
,而必须像这样使用wp_remote_get
:
I just found the problem. After debugging every bit item by item, the problem is that WordPress doesn't allow you to run file_get_contents
, instead you have to use wp_remote_get
like this:
GOOD: $json = wp_remote_get( $social['twitter'].$url );
BAD: $json = file_get_contents($social['twitter'].$url, false);
我只是浪费了一整天的时间,所以我希望它可以帮助其他人.
I just wasted a full day on this so I hope it helps someone else.
这篇关于WordPress 页面模板中的 PHP 代码不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!