X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Display latest Wordpress Posts (https://forum.x-cart.com/showthread.php?t=47980)

autobulbs 06-08-2009 02:44 AM

Display latest Wordpress Posts
 
This mod assumes that you use the Wordpress blogging software and uses the same database as your xcart software.
If your wordpress and x-cart use separate database then use this code for blog_headlines.php

If you do not use the default Wordpress SQL table prefix than you will need to specify it in the select query.
For example, change wp_posts to yourprefix_posts

Create xcart-root/include/blog_headlines.php and paste this in it:
Code:

<?php
$blog_headlines = func_query("SELECT post_title,guid,post_date as orderby, DATE_FORMAT(post_date,'%d/%m/%Y') as post_date FROM wp_posts WHERE post_status = 'publish' group by post_title order by orderby desc limit 3");
array("blog_headlines" => array());
$smarty->assign("blog_headlines", $blog_headlines);
?>


Create xcart-root/skin1/blog_headlines.tpl and paste this in it:
Code:

{* $Id: blog_headlines.tpl,v1 09/01/29  11:18:59 max Exp $ *}
{capture name=menu}
{foreach from=$blog_headlines item=bh}<a href="{$bh.guid}" class="blog_headlines">{$bh.post_title}</a><div class="blog_headlines">{$bh.post_date}</div><br class="blog_headlines"/><hr noshade="noshade" size="1" class="blog_headlines" />{/foreach}
{/capture}
{include file="menu.tpl" dingbats="dingbats_categorie.gif" menu_title="Blog Headlines" menu_content=$smarty.capture.menu}


Open xcart-root/init.php and add this at the end:
Code:

#Blog Headlines
include($xcart_dir."/include/blog_headlines.php");


Open xcart-root/skin1/skin1.css and add this:
Code:

/*** Blog Headlines Styles Start ***/
a.blog_headlines { /* The link */
    display:block;
}
a.blog_headlines:hover { /* The link when hovered over */
    background:#d7d7d7;
}
hr.blog_headlines { /* The line */
    color:#e1e1e1;
}
div.blog_headlines { /* The date */
    float:right;
}
br.blog_headlines{
    clear:left;
}
/*** Blog Headlines Styles End ***/


Open xcart-root/skin1/customer/home.tpl file and add this within VertMenuLeftColumn (or where ever appropriate):
Code:

{include file="blog_headlines.tpl"}

anandat 06-13-2009 01:23 AM

Re: Display latest Wordpress Posts
 
hi autobulbs,
This seems nice mod.
can you please tell me what I need to do if I am using different database for my x-cart & wordpress ?

Thanks.

crazyoval 06-16-2009 10:37 PM

Re: Display latest Wordpress Posts
 
HI autobulbs, nice one! what a great idea, bring x-cart sites up a sep.

just a tad too complicated for me to play with a live site. Do you have a live site running it today that I can see it working?

autobulbs 06-17-2009 12:28 AM

Re: Display latest Wordpress Posts
 
Hi crazyoval

Yes we have it running on http://www.bhpplus.co.uk/ (On the left, as Diesel News) Which is using the code in the original post.

And also on http://www.autobulbsdirect.co.uk/ (Under Latest Automotive News) But this is a heavily modified wordpress so the code is slightly different, but the principle is the same.

----------------------------------------------
anandat, I will look into that later this week :)

canuck 09-26-2009 02:34 PM

Re: Display latest Wordpress Posts
 
Quote:

Originally Posted by anandat
hi autobulbs,
This seems nice mod.
can you please tell me what I need to do if I am using different database for my x-cart & wordpress ?

Thanks.


I just ran across this post and I'm looking to do the same -- add most recent wordpress posts on my main x-cart home page. But my wordpress database is separate. Does anyone know how to do it?

I've seen rss mods but this seems like a cleaner way, no? Or does the separate database complicate things too much?

autobulbs 09-28-2009 03:52 AM

Re: Display latest Wordpress Posts
 
I did solve this, and here is how:

PHP Code:

<?php

#Connect to the wordpress database
db_connect('wp db host''wp db user''wp db pass');
db_select_db('wp db name');

#Select the posts
$blog_headlines func_query("SELECT post_title,guid,post_name,ID,DATE_FORMAT(post_date,'%d/%m/%Y') as post_date, post_date as orderbythis FROM wp_posts WHERE post_status = 'publish' GROUP BY post_title ORDER BY orderbythis DESC LIMIT 3;");

#Stuff them into a smarty array
array("blog_headlines" => array());
$smarty->assign("blog_headlines"$blog_headlines);

#Connect to the X-Cart database - these variables are in config.php
db_connect($sql_host$sql_user$sql_password);
db_select_db($sql_db);

?>


Scott DeToffol 10-10-2009 05:17 PM

Re: Display latest Wordpress Posts
 
I'm pulling in posts to X-cart sidebar using RSS via javascript from http://www.rssinclude.com/.

But I'm looking for a PHP solution. I think http://simplepie.org/ should be able to do it, but haven't dug in to figure it out yet.

anandat 10-10-2009 10:51 PM

Re: Display latest Wordpress Posts
 
Quote:

Originally Posted by Scott DeToffol
I'm pulling in posts to X-cart sidebar using RSS via javascript from http://www.rssinclude.com/.

But I'm looking for a PHP solution. I think http://simplepie.org/ should be able to do it, but haven't dug in to figure it out yet.


Thanks Scott DeToffol
rssinclude.com looks good as it's support twitter also. will try to play with this in next weekend.

Learner 12-29-2010 09:51 PM

Re: Display latest Wordpress Posts
 
Quote:

Originally Posted by autobulbs
This mod assumes that you use the Wordpress blogging software and uses the same database as your xcart software.
If your wordpress and x-cart use separate database then use this code for blog_headlines.php

If you do not use the default Wordpress SQL table prefix than you will need to specify it in the select query.
For example, change wp_posts to yourprefix_posts

Create xcart-root/include/blog_headlines.php and paste this in it:
Code:

<?php
$blog_headlines = func_query("SELECT post_title,guid,post_date as orderby, DATE_FORMAT(post_date,'%d/%m/%Y') as post_date FROM wp_posts WHERE post_status = 'publish' group by post_title order by orderby desc limit 3");
array("blog_headlines" => array());
$smarty->assign("blog_headlines", $blog_headlines);
?>


Create xcart-root/skin1/blog_headlines.tpl and paste this in it:
Code:

{* $Id: blog_headlines.tpl,v1 09/01/29  11:18:59 max Exp $ *}
{capture name=menu}
{foreach from=$blog_headlines item=bh}<a href="{$bh.guid}" class="blog_headlines">{$bh.post_title}</a><div class="blog_headlines">{$bh.post_date}</div><br class="blog_headlines"/><hr noshade="noshade" size="1" class="blog_headlines" />{/foreach}
{/capture}
{include file="menu.tpl" dingbats="dingbats_categorie.gif" menu_title="Blog Headlines" menu_content=$smarty.capture.menu}


Open xcart-root/init.php and add this at the end:
Code:

#Blog Headlines
include($xcart_dir."/include/blog_headlines.php");


Open xcart-root/skin1/skin1.css and add this:
Code:

/*** Blog Headlines Styles Start ***/
a.blog_headlines { /* The link */
    display:block;
}
a.blog_headlines:hover { /* The link when hovered over */
    background:#d7d7d7;
}
hr.blog_headlines { /* The line */
    color:#e1e1e1;
}
div.blog_headlines { /* The date */
    float:right;
}
br.blog_headlines{
    clear:left;
}
/*** Blog Headlines Styles End ***/


Open xcart-root/skin1/customer/home.tpl file and add this within VertMenuLeftColumn (or where ever appropriate):
Code:

{include file="blog_headlines.tpl"}


Hi I have x cart Pro 4.1.9 and my wordpress is in my same folder(within blog folder) of my domain with different database.After Adding codes
-#Blog Headlines
include($xcart_dir."/include/blog_headlines.php"); at the end
The entire store getting blank.How to solve this issue??

What is the exact codes for 4.1.9 Versions??

How to resolve this issue??

xtech 01-18-2011 09:52 PM

Re: Display latest Wordpress Posts
 
Hi,
Will it compatible with 4.4 version??

geckoday 01-19-2011 06:25 AM

Re: Display latest Wordpress Posts
 
Here's my solution using RSS and a Smarty plugin to pull the latest posts from any wordpress blog.

Create this file include/templater/plugins/function.sr_wpfeed.php:
PHP Code:

<?php
if ( !defined('XCART_START') ) { header("Location: home.php"); die("Access denied"); }
## -----------------------------------------------------------------------------
## Smarty plugin "sr_wpfeed"
## Purpose: Return wordpress RSS feed
##          Snow River X-Cart web store
##
## Author: Ralph Day, Snow River
##
##
## -----------------------------------------------------------------------------
##
## Changelog:
##
## 2009-10-26 Created (RD)
##
##
## -----------------------------------------------------------------------------
##
##
## Sample usage
## ------------
## {sr_wpfeed var="someposts" wpsite="http://www.snowriver.com/uggs-and-more" num=2}
## {foreach from=$someposts item=somepost}
##   <h2>
##     <a href="{$someposts.link}">
##       {$someposts.title}
##     </a>
##   </h2>
##   {$someposts.description}
##   <br>
## {/foreach}
## 
##
## Parameters
## ----------
## var [string] (required)
##      Name for Smarty variable to return feed array in
## 
## wpsite [string] (required)
##      URL of Wordpress site
## 
## num [integer] (required)
##      number of posts to return
## 
## 
## -----------------------------------------------------------------------------

function smarty_function_sr_wpfeed($params, &$smarty) {

  global 
$sql_tbl;
  global 
$store_language;

  
$required_params = array('var','wpsite''num');

  foreach(
$required_params as $_key => $reqd_param) {
    if (!isset(
$params[$reqd_param]))
      
$smarty->trigger_error("sr_wpfeed: required attribute '$reqd_param' not passed"E_USER_ERROR);
  }

  
$var      '';
  
$wpsite '';
  
$num 0;
  foreach(
$params as $_key => $_val) {
    switch(
$_key) {
      case 
'wpsite':
      case 
'var':
          if (!
is_array($_val)) {
              if (
$_val <> '')
                $
$_key $_val;
              else
                
$smarty->trigger_error("sr_wpfeed: '$_key' cannot be an empty string"E_USER_ERROR);
          } else
              
$smarty->trigger_error("sr_wpfeed: '$_key' cannot be an array"E_USER_ERROR);
          break;
          
      case 
'num':
          if (!
is_array($_val)) {
            $
$_key = (int)$_val;
            if ($
$_key || $$_key >25)
                
$smarty->trigger_error("sr_wpfeed: '$_key' not between 1 and 25"E_USER_ERROR);
          } else
              
$smarty->trigger_error("sr_wpfeed: '$_key' cannot be an array"E_USER_ERROR);
          break;

      default:
          
$smarty->trigger_error("sr_wpfeed: attribute '$_key' not recognized"E_USER_NOTICE);
          break;
    }
  }
    if(!
$xml=simplexml_load_file($wpsite.'/feed')){
        
$smarty->trigger_error('Error reading XML file',E_USER_ERROR);
    }
  
$posts = array();
    foreach(
$xml as $item){
        for(
$i=0$i<count($item->item); $i++){
            if(
$i<$num){
        
$posts[] = array('link'         =>  (string)$item->item[$i]->link
                       
'title'        =>  htmlentities($item->item[$i]->title,ENT_COMPAT'UTF-8')
                       , 
'description'  =>  $item->item[$i]->description
                       
);
      }
        }
    }

  
$smarty->assign($var$posts);
}
?>


Then in whatever template you want to display the posts in you can display the posts something like this:
Code:

{sr_wpfeed var="someposts" wpsite="http://www.snowriver.com/uggs-and-more" num=2}
{foreach from=$someposts item=somepost}
  <h2>
    <a href="{$someposts.link}">
      {$someposts.title}
    </a>
  </h2>
  {$someposts.description}
  <br>
{/foreach}


This should work in any version of X-Cart as it doesn't use any X-Cart functions. Also there are no modifications to X-Cart php code so it doesn't get wiped out by upgrades.

promart418 01-23-2011 07:10 AM

Re: Display latest Wordpress Posts
 
This didn't work for me. My site is *.co.uk and my blog is *.co.uk/blog/.

I copied everything 100%, then just changed the URL to match my wordpress blog homepage.

What did I do wrong?

promart418 01-23-2011 07:15 AM

Re: Display latest Wordpress Posts
 
p.s. I currently use simplepie to display RSS feed snippets. Could there be a conflict?

geckoday 01-24-2011 06:13 AM

Re: Display latest Wordpress Posts
 
Sorry, my example usage has a mistake. You need to use $somepost instead of $someposts inside the foreach loop like this:
Code:

{sr_wpfeed var="someposts" wpsite="http://www.snowriver.com/uggs-and-more" num=2}
{foreach from=$someposts item=somepost}
  <h2>
    <a href="{$somepost.link}">
      {$somepost.title}
    </a>
  </h2>
  {$somepost.description}
  <br>
{/foreach}


promart418 01-24-2011 01:45 PM

Re: Display latest Wordpress Posts
 
Hi,

I added:
Code:

{sr_wpfeed var="someposts" wpsite="http://www.promartsupplements.co.uk/blog" num=2}
{foreach from=$someposts item=somepost}
  <h2>
    <a href="{$somepost.link}">
      {$somepost.title}
    </a>
  </h2>
  {$somepost.description}
  <br>
{/foreach}


but I got the following errors:
Warning: simplexml_load_file() [function.simplexml-load-file]: http://www.promartsupplements.co.uk/blog//feed:1: parser error : Document is empty in /home/promarts/public_html/include/templater/plugins/function.sr_wpfeed.php on line 91
Error: Smarty error: Error reading XML file in /home/promarts/public_html/Smarty-2.6.19/Smarty.class.php on line 1092
Warning: Invalid argument supplied for foreach() in /home/promarts/public_html/include/templater/plugins/function.sr_wpfeed.php on line 95

Any ideas what is causing the errors?

Thanks,

geckoday 01-24-2011 02:50 PM

Re: Display latest Wordpress Posts
 
That's puzzling as http://www.promartsupplements.co.uk/blog//feed properly returns the XML data so it shouldn't be empty. If you have ssh access on your server what does this do?:

wget http://www.promartsupplements.co.uk/blog//feed

If that can't download the XML and you can in a browser check with your host to see why you can't load local URL's.

cflsystems 01-25-2011 03:19 PM

Re: Display latest Wordpress Posts
 
Just installed this for a client - works great. Thanks Ralph

cflsystems 01-25-2011 07:52 PM

Re: Display latest Wordpress Posts
 
Saw just one glitch - double and single quotes in description are converted to some other characters (I suspect some toher special characters are affected as well). I tried html_entity_decode and htmlspecialchars_decode but no luck. Do you have any idea Ralph?

geckoday 01-26-2011 06:53 AM

Re: Display latest Wordpress Posts
 
That's Wordpress converting quotes to smart quotes. Here's a link on how you can turn them off in Wordpress:

http://www.lancebledsoe.com/how-to-turn-off-wordpress-smart-quotes/

If you want to leave them as smart quotes on your wordpress blog but convert them back for the RSS feed you'll need to add some code to convert them. Here's a discussion of how that can be done.

http://shiflett.org/blog/2005/oct/convert-smart-quotes-with-php

cflsystems 01-26-2011 07:40 AM

Re: Display latest Wordpress Posts
 
Thanks Ralph

philrisk 02-25-2013 03:16 AM

Re: Display latest Wordpress Posts
 
Can you use this to call the image from the feed too?

Eyeglasses Expert 04-02-2015 04:49 PM

Re: Display latest Wordpress Posts
 
Quote:

Originally Posted by autobulbs
This mod assumes that you use the Wordpress blogging software and uses the same database as your xcart software.
If your wordpress and x-cart use separate database then use this code for blog_headlines.php

If you do not use the default Wordpress SQL table prefix than you will need to specify it in the select query.
For example, change wp_posts to yourprefix_posts

Create xcart-root/include/blog_headlines.php and paste this in it:
Code:

<?php
$blog_headlines = func_query("SELECT post_title,guid,post_date as orderby, DATE_FORMAT(post_date,'%d/%m/%Y') as post_date FROM wp_posts WHERE post_status = 'publish' group by post_title order by orderby desc limit 3");
array("blog_headlines" => array());
$smarty->assign("blog_headlines", $blog_headlines);
?>


Create xcart-root/skin1/blog_headlines.tpl and paste this in it:
Code:

{* $Id: blog_headlines.tpl,v1 09/01/29  11:18:59 max Exp $ *}
{capture name=menu}
{foreach from=$blog_headlines item=bh}<a href="{$bh.guid}" class="blog_headlines">{$bh.post_title}</a><div class="blog_headlines">{$bh.post_date}</div><br class="blog_headlines"/><hr noshade="noshade" size="1" class="blog_headlines" />{/foreach}
{/capture}
{include file="menu.tpl" dingbats="dingbats_categorie.gif" menu_title="Blog Headlines" menu_content=$smarty.capture.menu}


Open xcart-root/init.php and add this at the end:
Code:

#Blog Headlines
include($xcart_dir."/include/blog_headlines.php");


Open xcart-root/skin1/skin1.css and add this:
Code:

/*** Blog Headlines Styles Start ***/
a.blog_headlines { /* The link */
    display:block;
}
a.blog_headlines:hover { /* The link when hovered over */
    background:#d7d7d7;
}
hr.blog_headlines { /* The line */
    color:#e1e1e1;
}
div.blog_headlines { /* The date */
    float:right;
}
br.blog_headlines{
    clear:left;
}
/*** Blog Headlines Styles End ***/


Open xcart-root/skin1/customer/home.tpl file and add this within VertMenuLeftColumn (or where ever appropriate):
Code:

{include file="blog_headlines.tpl"}


i have tried this method under x-cart v4.6.6 for www.glassespeople.com
i can get the wordpress posts, but i got another problem. my sites verifying code does not shows, all image under admin panel does not display


All times are GMT -8. The time now is 04:05 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.