codeigniter 致命错误:无法重新声明site_url()

dm7nw8vv  于 2023-03-21  发布在  其他
关注(0)|答案(2)|浏览(407)

我在codeigniter工作,我想在我的网站上获取博客文章
网站主页当我包括两个文件

require_once('blog/wp-blog-header.php');
require_once('blog/wp-includes/link-template.php');

我得到了一个错误。

Fatal error: Cannot redeclare site_url() (previously declared in /home/homebidpro/public_html/dev/system/helpers/url_helper.php:83) in /home/homebidpro/public_html/dev/blog/wp-includes/link-template.php on line 3004

我根据位置文件注解了此函数:

function site_url( $path = '', $scheme = null ) {
    return get_site_url( null, $path, $scheme );
}

这个错误被删除,网站的所有功能wotks,但当我打开博客是不打开它是给错误

dev.homebidpro.com页面无法正常工作

博客和项目位置相同,均位于开发文件夹中。
我卡到现在了,请尽快给予我解决办法。
代码的其余部分是

public function blogData()
    {

        require_once('blog/wp-blog-header.php');
        require_once('blog/wp-includes/link-template.php');

        if($catagory !='')
        {

            $args = array(  'posts_per_page' => 3, 'category_name' => $catagory );
        }
        else
        {

            $args = array('posts_per_page' => 4);
        }    

        $mypostsMov = get_posts( $args );  
       // echo "<pre>"; print_r($mypostsMov); die;
        $mypostsarrayMov = array();
        $i=0;
        foreach ($mypostsMov as $post ) : 
        $excerpt = preg_replace("/<img[^>]+\>/i", "", $post->post_content);      
        $mypostsarrayMov[$i]['post_title'] = strip_tags($post->post_title);
        $mypostsarrayMov[$i]['content'] =    strip_tags($post->post_content);
        $mypostsarrayMov[$i]['permalink'] = get_permalink($post->ID);
        $mypostsarrayMov[$i]['thumbnail'] =get_the_post_thumbnail($post->ID,array( 300, 200));
        $i++;
        endforeach; 
        wp_reset_postdata();

         //echo "<pre>"; print_r($mypostsarrayMov); die;

         //return $this->render('MovePlusServiceBundle:Default:recentpostCombined.html.twig',array('mypostsMov'=>$mypostsarrayMov, ));

         return $mypostsarrayMov;

}

codeigniter中的site_url函数

if ( ! function_exists('site_url'))

{

    function site_url($uri = '')
    {
        $CI =& get_instance();
        return $CI->config->site_url($uri);
    }

}

还有siteurl函数,我已经发给你的博客文件夹。

798qvoo8

798qvoo81#

您遇到的问题是CodeIgniter在url助手中有一个site_url()函数,而WordPress也有一个在wp-includes/link-template.php file中定义的site_url()函数。
因为您已经在CodeIniter页面中包含了link-template.php,PHP会告诉您您正在尝试创建一个已经存在的函数,但您不能这样做。
在我看来,你要么:

  • 不在要包含Wordpress的页面上加载CodeIgniter URL帮助器,或者
  • 加载CodeIgniter url帮助器后,不加载wp-includes/link-template.php文件,或者
  • 将wp-includes/link-template.php文件中所需的功能复制到自定义库/helper或控制器中,以便生成需要包含该文件的结果。

您可以选择实现这些功能的组合

yh2wf1be

yh2wf1be2#

已找到一个新的解决方法,可修复WP 6+和CI4.3的此问题
更改wp-includes中的link-template.php,添加以下内容:

if (!function_exists('site_url')) {
    function site_url($path = '', $scheme = null)
    {
        return get_site_url(null, $path, $scheme);
    }
}

这使得管理和WordPress环境不受CodeIgniter的影响,但它使所有功能正常运行。测试成功。希望它对试图将这两个结合起来的人有用。

相关问题