【发布时间】:2013-07-05 18:21:01
【问题描述】:
我正在 wordpress 中编写一个自定义插件,并且我是第一次使用简码。 到目前为止,我的简码有效,但现在我想在该简码的函数中执行查询,以获取特定员工组 ID 中的所有员工
[employee_group_tag group_id=2]
这是具有我的简码功能的文件:
<?php
/*
Plugin Name: Employees
Plugin URI: xxx
Description: xxx
Version: 1.0
Author: xxx
Author URI: http://www.blabla.com
*/
global $wpdb;
function employee_shortcode_func( $atts ) {
extract( shortcode_atts( array(
'group_id' => '0',
), $atts ) );
// Alle werknemers ophalen adhv van group_id
$sql = 'SELECT * FROM wp_werknemers_employees WHERE employee_employee_group_id = ' . $group_id;
$results = $wpdb->get_results($sql);
return 'test';
}
add_shortcode( 'employee_group_tag', 'employee_shortcode_func' );
但是当我运行它时,它会卡在 $wpdb->get_results($sql) 上,因为当我离开它时,它会正确显示我的页面,但是当我想用员工详细信息填充它时,我只能得到我页面的一半.所以它“打破”了
我尝试过(适用于我所有其他文件)
require_once("../../../wp-config.php")
但它不起作用......
是否有可能因为它在我的“主”插件文件中而无法正常工作?因为在我所有其他文件中,当我使用 require 函数时,我可以使用 wpdb...
有什么想法吗?
【问题讨论】:
-
global wpdb属于函数内部。 -
天哪……没想到,你救了我的命! :)