我是using this code here从我的Minecraft服务器获取信息。它使用静态.php文件按预期方式执行,但在将其移动到WordPress的本地副本后,我收到以下错误:
Fatal error: Call to a member function GetPlayers() on a non-object in C:\...\home.php on line 125
我很确定我正确地移动了一切。第125行是这段代码:
<div class="online group container">
<h2> Who's online </h2>
<?php if( ( $Players = $Query->GetPlayers( ) ) !== false ): ?>
<?php foreach( $Players as $Player ): ?>
<div class="player">
<span class="p" title="<?php echo htmlspecialchars( $Player ); ?>" data-player="<?php echo htmlspecialchars( $Player ); ?>">☺</span>
</div><!-- /.player -->
这就是我在doctype之前调用header.php中的PHP类的方法。
<?php
define( 'MQ_SERVER_ADDR', '108.170.3.138:25643' );
define( 'MQ_SERVER_PORT', 25643 );
define( 'MQ_TIMEOUT', 1 );
require bloginfo('template_url') . 'inc/avatars/MinecraftQuery.class.php';
$Timer = MicroTime( true );
$Query = new MinecraftQuery( );
try
{
$Query->Connect( MQ_SERVER_ADDR, MQ_SERVER_PORT, MQ_TIMEOUT );
}
catch( MinecraftQueryException $e )
{
$Error = $e->getMessage();
echo 'error. <br>'. $Error;
}
?>
这很奇怪,因为它在没有WordPress的情况下效果很好,但是一旦我移动它,就会破坏它。我做错了什么?
答案 0 :(得分:1)
创建将在
中返回Query的函数function.php
function get_MQ_Query() {
define( 'MQ_SERVER_ADDR', '108.170.3.138:25643' );
define( 'MQ_SERVER_PORT', 25643 );
define( 'MQ_TIMEOUT', 1 );
require bloginfo('template_url') . 'inc/avatars/MinecraftQuery.class.php';
$Timer = MicroTime( true );
$Query = new MinecraftQuery( );
try
{
$Query->Connect( MQ_SERVER_ADDR, MQ_SERVER_PORT, MQ_TIMEOUT );
}
catch( MinecraftQueryException $e )
{
$Error = $e->getMessage();
echo 'error. <br>'. $Error;
}
return $Query;
}
home.php
$Query = get_MQ_Query();
?><div class="online group container">
<h2> Who's online </h2>