Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using WP-ecommerce to build a shop, and have a random product generator on the left and right sidebar. So the same product doesnt appear twice on the page, I have got the post id's from each product in the left sidebar, and excluded them from the product query in the right sidebar.

I am doing this using an array with array_push:

<?php $leftids = array(); ?>

<?php query_posts('post_type=wpsc-product&showposts=2&orderby=rand&wpsc_rand=i-want-this-product-to-be-random'); if (have_posts()) : while (have_posts()) : the_post(); ?>

<div style="display:none"><?php the_title(); ?></div>

<a href="<?php the_permalink();?>"><?php the_post_thumbnail('sidebarimg');?></a>

<h2><?php the_title(); ?></h2>

<?php  array_push($leftids, $post->ID); ?>
<?php echo $leftids[0]; ?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?> 

The problem is I cant access this array when on a product page at all. It works fine on a normal wordpress page such as home or contact us.

Any ideas?

Thanks

share|improve this question

closed as off-topic by Pieter Goosen, cybmeta, Mark Kaplun, birgire, kaiser Dec 4 at 16:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 0 down vote accepted

not sure where the code snippet is placed, and where you are trying to access it from. A simple (but ugly) solution might simply be to set this array to be global.

Something like

$GLOBALS['leftids'] = $leftids;

and then from the other code use

global $leftids;
echo $leftids[0];
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.