View Single Post
  #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