Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Display latest Wordpress Posts

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #11  
Old 01-19-2011, 06:25 AM
 
geckoday geckoday is offline
 

X-Wizard
  
Join Date: Aug 2005
Posts: 1,073
 

Default 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.
__________________
Manuka Bay Company
X-Cart Version 4.0.19 [Linux]

UGG Boots and other fine sheepskin products
http://www.snowriver.com
Reply With Quote

The following 4 users thank geckoday for this useful post:
am2003 (01-19-2011), chrischall (07-18-2013), pauline (08-02-2012), philrisk (03-06-2013)
  #12  
Old 01-23-2011, 07:10 AM
  promart418's Avatar 
promart418 promart418 is offline
 

Senior Member
  
Join Date: Jan 2009
Location: Leeds, West Yorkshire, England
Posts: 137
 

Default 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?
__________________
x-cart version - 5

Martin Procter

Promart Supplements
Nicola Anne Photography
Reply With Quote
  #13  
Old 01-23-2011, 07:15 AM
  promart418's Avatar 
promart418 promart418 is offline
 

Senior Member
  
Join Date: Jan 2009
Location: Leeds, West Yorkshire, England
Posts: 137
 

Default Re: Display latest Wordpress Posts

p.s. I currently use simplepie to display RSS feed snippets. Could there be a conflict?
__________________
x-cart version - 5

Martin Procter

Promart Supplements
Nicola Anne Photography
Reply With Quote
  #14  
Old 01-24-2011, 06:13 AM
 
geckoday geckoday is offline
 

X-Wizard
  
Join Date: Aug 2005
Posts: 1,073
 

Default 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}
__________________
Manuka Bay Company
X-Cart Version 4.0.19 [Linux]

UGG Boots and other fine sheepskin products
http://www.snowriver.com
Reply With Quote
  #15  
Old 01-24-2011, 01:45 PM
  promart418's Avatar 
promart418 promart418 is offline
 

Senior Member
  
Join Date: Jan 2009
Location: Leeds, West Yorkshire, England
Posts: 137
 

Default 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,
__________________
x-cart version - 5

Martin Procter

Promart Supplements
Nicola Anne Photography
Reply With Quote
  #16  
Old 01-24-2011, 02:50 PM
 
geckoday geckoday is offline
 

X-Wizard
  
Join Date: Aug 2005
Posts: 1,073
 

Default 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.
__________________
Manuka Bay Company
X-Cart Version 4.0.19 [Linux]

UGG Boots and other fine sheepskin products
http://www.snowriver.com
Reply With Quote
  #17  
Old 01-25-2011, 03:19 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Re: Display latest Wordpress Posts

Just installed this for a client - works great. Thanks Ralph
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #18  
Old 01-25-2011, 07:52 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default 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?
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #19  
Old 01-26-2011, 06:53 AM
 
geckoday geckoday is offline
 

X-Wizard
  
Join Date: Aug 2005
Posts: 1,073
 

Default 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
__________________
Manuka Bay Company
X-Cart Version 4.0.19 [Linux]

UGG Boots and other fine sheepskin products
http://www.snowriver.com
Reply With Quote

The following user thanks geckoday for this useful post:
cflsystems (01-26-2011)
  #20  
Old 01-26-2011, 07:40 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Re: Display latest Wordpress Posts

Thanks Ralph
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 02:33 PM.

   

 
X-Cart forums © 2001-2020