gpt4 book ai didi

Wordpress 自定义插件简码 wpdb 未知

转载 作者:行者123 更新时间:2023-12-04 00:48:41 25 4
gpt4 key购买 nike

我正在 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 ...

有什么想法吗?

最佳答案

来自PHP Manual :

For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:

$a = 1;
include 'b.inc';

Here the $a variable will be available within the included b.inc script. However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example:

$a = 1; /* global scope */ 

function test()
{
echo $a; /* reference to local scope variable */
}

test();

最后一个示例不起作用,因为 $a 未在函数范围内定义。

就像您的情况一样,要使其正常工作,您必须使用:

function employee_shortcode_func( $atts ) {
global $wpdb;
// etc
}

关于Wordpress 自定义插件简码 wpdb 未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17526285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com