我'm able to use Wordpress functions in my CI controllers without my URLs being messed up when adding the following code to the top of CI' s index.php :
require_once './wordpress/wp-blog-header.php';
add_filter('site_url', 'ci_site_url', 1);
function ci_site_url($uri = '') {
$CI =& get_instance();
$uri = ltrim(str_replace($CI->config->base_url('wordpress/'), '', $uri),'/'); // "wordpress/" is in my case the name of the directory where I installed Wordpress. See directory structure above.
return $CI->config->site_url($uri);
}
5 回答
第一步是将CodeIgniter和WordPress文件移动到它们自己的目录中 .
之后,将以下行放在CodeIgniter的
index.php
文件的顶部 . 根据需要将路径更改为wp-blog-header.php
以指向您的WordPress的根目录 .然后,您可以在视图中使用以下功能:
其他帮助函数也可以在WordPress的文档中找到,它可以帮助您集成设计 .
当我在Codeigniter的index.php页面中包含文件wp-blog-header.php时,我遇到了一个问题,即在codeigniter的URL帮助器和WordPress中都定义了site_url() . 我使用以下代码解决了这个问题:
最后一行需要添加,因为WordPress文件正在向标头添加HTTP响应标头'HTTP / 1.0 404 Page not found' .
现在可以使用WordPress函数在CodeIgntier中调用 .
这是在codeigniter项目中使用WordPress模板的另一种方法 . 这对我来说效果更好,所以我想分享它 . 使用WordPress 3.3.1和Codeigniter 2.1进行测试 .
目录结构:
/ci/index.php(CI索引文件的顶部)
通过覆盖默认的codeigniter版本来处理site_url函数冲突 . 您需要更改在codeigniter中使用
site_url()
的任何地方,而不是使用ci_site_url()
./ci/application/helpers/MY_url_helper.php
您现在可以使用WordPress
get_header()
和/或get_footer()
函数在CI项目中绘制模板 .我正在使用Wordpress来管理自定义CI电子商务网站中的文章 . CI是我的主要网站 . 目录结构如下:
我'm able to use Wordpress functions in my CI controllers without my URLs being messed up when adding the following code to the top of CI' s index.php :
在使用JérômeJaglale(http://jeromejaglale.com/doc/php/codeigniter_i18n)的CI i18n库时也可以使用 .
如果您计划在代码中使用代码ignitor site_url函数,或者您正在合并现有CI网站和WP ...这可能会有所帮助:
在CI index.php的顶部:
有效地,这允许您在CI中使用site_url,因此如果您已经为项目添加了大量链接和内容,它可能会帮助您 .