简体   繁体   English

将Wordpress与Opencart集成

[英]Integrating Wordpress with Opencart

I have an opencart shop and wordpress installation running on the same server and I would like to grab a few articles and show them on the product page in opencart. 我有一个opencart商店和wordpress安装程序在同一台服务器上运行,我想获取一些文章并将其显示在opencart的产品页面上。

Here is the code I inserted on my product page template, however I'm having problems: 这是我在产品页面模板上插入的代码,但是出现问题:

<?php
require('blog/wp-blog-header.php');
?>

<?php
$posts = get_posts('numberposts=3&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>    
<?php the_excerpt(); ?> 
<?php
endforeach;
?>

I'm gettings this error 我收到此错误

Notice: Constant DB_PASSWORD already defined in /home/admin/web/domain.com/public_html/blog/wp-config.php on line 29 ERROR ESTABLISHING A DATABASE CONNECTION 注意:在第29行的/home/admin/web/domain.com/public_html/blog/wp-config.php中已定义的常量DB_PASSWORD 错误建立数据库连接

I know DB_PASSWORD is also used by the opencart config, is this the problem? 我知道DB_PASSWORD也由opencart配置使用,这是问题吗? And more importantly is there a solution to this problem? 更重要的是,是否有解决此问题的方法?

i think there is a better way to fetch posts from WordPress. 我认为有一种更好的方法可以从WordPress获取帖子。

use wp-api to get your posts in json format. 使用wp-api获取json格式的帖子。 then you can process on it as you want. 然后您可以根据需要对其进行处理。

here is a simple function in php (i have used it as a helper in CodeIgniter. 这是php中的一个简单函数(我已在CodeIgniter中将其用作帮助程序。

    function blog_posts($site_url = 'http://yoursite.com/', $cat_id = 1, $count = 5, $thumbnails = true)
{

    $url = $site_url . 'wp-json/wp/v2/posts?';
    $url_data = [
        'categories' => $cat_id,
        'per_page'   => $count,
    ];
    $url_data = http_build_query($url_data, 1, '&');
    if ($thumbnails) {
        $url_data = $url_data . '&_embed';
    }
    $final_url = $url . $url_data;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $final_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    return $result;
}

An even simpler way could be to use RSS. 甚至更简单的方法是使用RSS。

Most blogs should produce an RSS feed containing the content you need - this will be quicker and easier to get than an API. 大多数博客都应生成包含所需内容的RSS feed-与API相比,这将更快,更容易获得。

You can then see this answer about how the parse the RSS XML from PHP: Best way to parse RSS/Atom feeds with PHP 然后,您可以看到有关如何从PHP解析RSS XML的答案: 使用PHP解析RSS / Atom提要的最佳方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM