如何在PHP中使用输入变量调用函数?
有两个参数可以将对象作为JSON返回 我尝试通过Id获取帖子并附加到数组:如何在PHP中使用输入变量调用函数?,php,wordpress,function,undefined,Php,Wordpress,Function,Undefined,有两个参数可以将对象作为JSON返回 我尝试通过Id获取帖子并附加到数组: // Get Considered Posts, Training, Service, Intall public function get_considered_posts() { $item = new stdclass(); $item->msg = 'Empty'; $item->status = false; $result = Array(); $post
// Get Considered Posts, Training, Service, Intall
public function get_considered_posts() {
$item = new stdclass();
$item->msg = 'Empty';
$item->status = false;
$result = Array();
$post = $get_post_by_id( 156 ); // Training
if ( $post ) {
$result[] = $post ;
}
$post = $get_post_by_id( 164 ); // Service
if ( $post ) {
$result[] = $post ;
}
$post = $get_post_by_id( 161 ); // Intall
if ( $post ) {
$result[] = $post ;
}
if (count( $result ) == 0 ) {
$item->msg = 'Empty';
$item->status = false;
} else {
$item->msg = 'Success';
$item->status = true;
}
$item->result = $result;
return $item;
}
在这里,我只想使用每个帖子的5个属性:
// Get Post By Id
private function get_post_by_id( $pid ) {
$post = get_post( $pid = 0 );
if ( $post ) {
if ($post->post_status == 'publish') {
$object = new stdclass();
$object->id = $post->ID;
$object->cid = $pid;
$object->title = $post->post_title;
$object->content = $post->post_content;
$object->image = ''.wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
return $object;
}
}
return $post;
}
但它显示500内部服务器错误
当我检查error\u log
时,它会显示:
[17-May-2016 14:27:54 UTC] PHP Notice: Undefined variable: get_post_by_id in /home/codypars/public_html/app/wp-webservice/helper.php on line 62
[17-May-2016 14:27:54 UTC]PHP致命错误:第62行的/home/codypars/public_html/app/wp webservice/helper.PHP中的函数名必须是字符串
我怎么能修好它
解决方法:谢谢你,还有。要在PHP类中调用自己的函数,正确的语法是:
this->myFunctionName( $Param1, $Param2, $Param3, ...);
调用函数名时,从函数名前面删除$ e、 g 应该是
$post = get_post_by_id( 164 );
调用函数名时,从函数名前面删除$ e、 g 应该是
$post = get_post_by_id( 164 );
而
$this->
在哪里?是的,$post=$this->get\u post\u by_id(156),谢谢你,$this->
在哪里?是的,$post=$this->get\u post\u by_id(156),非常感谢。