2012-02-22 49 views
0

我使用jcart http://conceptlogic.com/jcart/在wordpress中创建一些购物车功能。Wordpress + PHP:在另一个函数中使用带in_array的数组变量

我想在我的functions.php文件中的另一个函数内访问一个jcart函数。

foreach($jcart->get_contents() as $item) { 
    $psitems[] = $item['id']; 
} 

此代码工作正常上的functions.php本身或page--它创建$psitems阵列,所以我可以检查,如果该数组包含特定值的任何模板。现在

,下面的函数中:

function gallery_shortcode_fancybox($attr) { ... 

我有以下几点:

foreach($jcart->get_contents() as $item) { 
    $psitems[] = $item['id']; 
} 

foreach ($attachments as $id => $attachment) { 
    $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_image($id, 'full', false, false) : wp_get_attachment_image($id, 'full', true, false); 

    $output .= "<div class='property'>"; 
    $output .= "$link"; 
    $output .= ' 
     <div class="property-details"> 
      <div class="property-details-inner"> 

       <form method="post" action="" class="jcart"> 
        <fieldset> 
         <input type="hidden" name="jcartToken" value="' . $_SESSION['jcartToken'] . '" /> 
         <input type="hidden" name="my-item-id" value="' . $id . '" /> 
         <input type="hidden" name="my-item-name" value="Photo #' . $id . '" /> 
         <input type="hidden" name="my-item-url" value="' . wp_get_attachment_url($id) . '" /> 
         <input type="hidden" name="my-item-qty" value="1" size="3" />'; 

    if(in_array($id,$psitems)) { 
     $output .= '<span class="action terminal pull-sheet">Image in Pullsheet</span>' 
    } else { 
     $output .=   '<input type="submit" name="my-add-button" value="Add to Pull Sheet" class="action terminal pull-sheet" />'; 
    } 
    $output .=    '</fieldset> 
       </form> 
      </div> <!-- property-details-inner --> 
     </div>'; 
    $output .= "</div>"; 
} 

return $output; 

我收到以下错误:

Call to a member function get_contents() on a non-object

如果我尝试把原来的foreach($jcart->get_contents() as $item) { ...代码在函数外面,我得到以下错误:

Warning: in_array() expects parameter 2 to be array, null given in... 

如何在函数内部访问这个数组并避免这些错误?

谢谢!

回答

1

关于第一个错误,这意味着你必须初始化$ jcart。

至于警告,试试这个:使用初始化函数从原来的jcart.php脚本

$array = $jcart->get_contents(); 
foreach ($array as $item) ... 
+0

完美工作,感谢 – 2012-02-22 21:56:42

相关问题