在不同的 php 文件中访问 WordPress 功能? [英] Access WordPress functions in a different php file?
问题描述
如何在不同的文件(例如 x.php)中调用内置的 WordPress 函数(特别是 wp_get_user()
)?
How am I able to call the built-in WordPress functions (specifically wp_get_user()
) in a different file (say, x.php)?
这是我的情况.如果我在我的主题目录中的 index.php 文件上调用 wp_get_current_user()
,它就可以完美运行.
Here's my situation. If I call wp_get_current_user()
on the index.php file within my theme directory, it works just perfectly.
但是,如果我有 x.php 并调用它,我会收到错误消息.我用谷歌搜索,现在包括 wp-load.php
(在它的正确路径).现在它只是给了我一个没有值的关联数组.
However, if I have x.php and called that, I got an error. I googled around, and am now including wp-load.php
(at it's correct path). Now it's just giving me an associated array with no values.
关联数组为:
WP_User Object ( [data] => [ID] => 0 [id] => 0 [caps] => Array ( ) [cap_key] => [roles] => Array ( ) [allcaps] => Array ( ) [first_name] => [last_name] => [filter] => )
我尝试在这个文件中包含 index.php
,但它仍然吐出上面的那个东西,而不是正确的信息(由索引上的 print_r
显示.php).
I tried including index.php
in this file, and it still spits out that stuf above, instead of the correct information (which is shown by a print_r
on index.php).
谢谢!
推荐答案
尝试同时包含标题 <?php require('{correct_path}/wp-blog-header.php');
如果这不起作用,也可以尝试在 wordpress 设置 php 中重新声明用于 global $var
的变量.
try also including the header <?php require('{correct_path}/wp-blog-header.php');
and if that doesn't work also try re-declaring the variables your using to global $var
in your wordpress setting php.
这篇关于在不同的 php 文件中访问 WordPress 功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!