0

I have here the code but how can I set it with a specific category

like "Electronic" which has an id of "1"

here is the code

<?php 
// get the product categories
$product_categories = wp_get_object_terms( wpsc_the_product_id(), 'wpsc_product_category', array('fields' => 'ids') );
// arguments
$args = array(
'post_type' => 'wpsc-product',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'rand',
'tax_query' => array(
    array(
        'taxonomy' => 'wpsc_product_category',
        'field' => 'id',
        'terms' => $product_categories
    )
)
);
$related_products = new WP_Query( $args );
// loop over query
if ($related_products->have_posts()) :
echo '<ul>';
while ( $related_products->have_posts() ) : $related_products->the_post();
?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
echo '</ul>';
endif;
// Reset Post Data
wp_reset_postdata();
?>

it displays the products on that page, I need to display products based on what category I put in so that it will be different from the page products

1

1 Answer 1

0

Select posts only from that category, with a code like this one in the Loop:

query_posts(array('category_name' => 'Electronic', 'showposts' => '1'));
if (have_posts()): while (have_posts()) : the_post();
...

UPDATED Example:

<?php
query_posts( array( 'category_name' => 'Electronic',
                    'showposts'     => '1' ) );  // Replace '1' with posts quantity per page
echo '<ul>';
if ( have_posts() ): while ( have_posts() ) : the_post();
  ?>

<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>

<?php
endwhile;
  echo '</ul>';
endif;
// Reset Post Data
wp_reset_postdata();
?>

I think the last code can replace the script of the question. Not sure, though, because can't test it.

Note: Of course you would have to create the category and assign posts to that category, or use the existing category: wpsc_product_category.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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