有谁可以告诉我为什么wordpress'页面模板'中的代码不会执行?
'Echo'似乎有效,但所有'include'和'print_r()'以及其他功能都没有。这个确切的代码适用于我的家庭服务器,但不适用于wordpress托管网站:
<?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]';
?>
答案 0 :(得分:0)
没有足够的代表发表评论,但您是否检查了来源? WordPress会默默地死去,但源代码中经常会有一些东西。
简短的回答,是的,你可以在模板中执行你想要的任何php。我只是将它粘贴到我们测试服务器上的WordPress模板中,运行正常。
答案 1 :(得分:0)
我刚发现了这个问题。逐项调试每个位后,问题是WordPress不允许您运行file_get_contents
,而是必须使用wp_remote_get
这样:
GOOD: $json = wp_remote_get( $social['twitter'].$url );
BAD: $json = file_get_contents($social['twitter'].$url, false);
我只是浪费了整整一天的时间,所以我希望它可以帮助别人。