0

我在这里有代码,但是如何将其设置为特定类别

比如 ID 为“1”的“电子”

这是代码

<?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();
?>

它在该页面上显示产品,我需要根据我放入的类别显示产品,以便它与页面产品不同

4

1 回答 1

0

仅从该类别中选择帖子,在循环中使用如下代码:

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

更新 示例:

<?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();
?>

我认为最后的代码可以替换问题的脚本。不过不确定,因为无法测试。

注意:当然,您必须创建类别并将帖子分配给该类别,或使用现有类别:wpsc_product_category

于 2012-11-13T05:37:52.350 回答