0

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.

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

I know DB_PASSWORD is also used by the opencart config, is this the problem? And more importantly is there a solution to this problem?

Florin C.
  • 266
  • 7
  • 18

2 Answers2

0

i think there is a better way to fetch posts from WordPress.

use wp-api to get your posts in json format. 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.

    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;
}
Ali Qorbani
  • 1,254
  • 8
  • 27
0

An even simpler way could be to use RSS.

Most blogs should produce an RSS feed containing the content you need - this will be quicker and easier to get than an 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

Paul Feakins
  • 719
  • 5
  • 7