php – WordPress oop wpdb在课堂上
作者:互联网
我是OOP PHP的新手,我试图在我的一些自定义类中使用$wpdb(WORDPRESS)对象,但不知道如何做到这一点.每次我尝试用$wpdb实现基本操作都会导致失败.
我需要一些基本的东西,比如get_results(),….
那么如何做这样的事情:
global $wpdb;
$my_custom_table = $wpdb->prefix . "table_name";
$table_content = $wpdb->get_results("SELECT * FROM ".$my_custom_table);
并将它放入我的班级,如下所示:
Class MyClass{
public function table_results(){
//put in here
return $this->table_content;
}
}
我需要在单独的文件中使用该类,以便我可以轻松调用它.
解决方法:
试试这个…
<?php
class MyClass {
private $wpdb;
public function __construct()
{
global $wpdb;
$this->wpdb = $wpdb;
}
public function table_results(){
$my_custom_table = $this->wpdb->prefix . "table_name";
$table_content = $this->wpdb->get_results("SELECT * FROM $my_custom_table");
return $table_content;
}
}
标签:php,class,oop,wordpress,wpdb 来源: https://codeday.me/bug/20190823/1695154.html