在我的 PHP 应用程序中访问 WordPress API
我从 PHP 代码中访问 WordPress API,仅包含 wp-blog-header.php 并使用 get_posts()。
当我的 PHP 代码与 WP 目录处于同一级别时,这可以正常工作。例如:
/blog/[..wp files..]
/index.php
在index.php,我有:
require('blog/wp-blog-header.php');
$post = get_posts(...);
并且工作得很好。
但是,当我尝试对目录内的代码执行相同操作时,例如:
/blog/[..wp files..]
/folder/index.php
在folder/index.php 中,我有:
require('../blog/wp-blog-header.php');
$post = get_posts(...);
这总是使我的应用程序重定向到 WP 安装(wp-admin/install.php)并且不起作用。
有什么想法吗?可以在目录中使用 API 吗?
该博客配置正确并且本身运行正常。
编辑:堆栈跟踪显示在文件夹内使用时应用程序何时重定向到 wp_not_installed()
6 0.4052 3402748 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-load.php' ) ../class_wordpress.php:11
7 0.4054 3408296 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-config.php' ) ../wp-load.php:30
8 0.4063 3487064 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-settings.php' ) ../wp-config.php:19
9 1.3650 6103276 wp_not_installed( ) ../wp-settings.php:100
10 1.6258 7676148 wp_redirect( ) ../load.php:408
11 1.6261 7684480 header ( ) ../pluggable.php:890
谢谢!
I'm accessing the WordPress API from within my PHP code just including the wp-blog-header.php and using get_posts().
This is working ok when my PHP code is at the same level than the WP directory. For example:
/blog/[..wp files..]
/index.php
At index.php, I have:
require('blog/wp-blog-header.php');
$post = get_posts(...);
and is working pretty well.
BUT, when I try to do the same with the code inside a directory, for example:
/blog/[..wp files..]
/folder/index.php
and at folder/index.php I have:
require('../blog/wp-blog-header.php');
$post = get_posts(...);
this always makes my app to redirect to the WP installation (wp-admin/install.php) and doesn't work.
Any idea ? Can the API be used from within a directory ?
The blog is configured correctly and is working ok by itself.
Edit: Stack trace showing when the app redirects to wp_not_installed() when used inside a folder
6 0.4052 3402748 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-load.php' ) ../class_wordpress.php:11
7 0.4054 3408296 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-config.php' ) ../wp-load.php:30
8 0.4063 3487064 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-settings.php' ) ../wp-config.php:19
9 1.3650 6103276 wp_not_installed( ) ../wp-settings.php:100
10 1.6258 7676148 wp_redirect( ) ../load.php:408
11 1.6261 7684480 header ( ) ../pluggable.php:890
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于在 WordPress 系统外部访问的所有 WordPress,您必须包含
wp-load.php
For everything wordpress that is accessed outside wordpress system, you HAVE to include
wp-load.php
我认为@silent意味着你需要将两者都包含在内才能工作。然而,当我运行 WordPress 时,我需要两者。
I think @silent means that you need to have both included for it to work. I however need both when I'm running WordPress.
看看这个 http: //www.webopius.com/content/139/using-the-wordpress-api-from-pages-outside-of-wordpress
添加:
显然在之前
就可以了。
Take a look at this http://www.webopius.com/content/139/using-the-wordpress-api-from-pages-outside-of-wordpress
Apparently adding:
right before:
does the trick.