如何在不覆盖现有Woo-Commerce的情况下在自定义页面中按ID显示Single Product-4数据

时间:2018-08-11 作者:Amit Jana

I\'m trying to show Single Product Data in Custom Page by given Category ID but when I used the below code:

<?php $args = array( \'post_type\' => \'product\', \'product_cat\' => \'category-id\' ); if ( ! defined( \'ABSPATH\' ) ) { exit; // Exit if accessed directly } get_template_part( \'woocommerce/archive-product\', \'page\' ); ?>

It only fetch data like this when I insert print_r(query_posts( $args ))

then I got

Array ( [0] => WP_Post Object ( [ID] => 391 [post_author] => 1 [post_date] => 2018-08-03 08:55:15 [post_date_gmt] => 2018-08-03 08:55:15 [post_content] => [post_title] => 4 Bars [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => closed [post_password] => [post_name] => 4-bars [to_ping] => [pinged] => [post_modified] => 2018-08-03 09:09:22 [post_modified_gmt] => 2018-08-03 09:09:22 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/abc/?post_type=product&p=391 [menu_order] => 0 [post_type] => product [post_mime_type] => [comment_count] => 0 [filter] => raw ) )

so, If you consider that [guid] => link, which will re-direct me in single-product.php page, so, I want the all single product data shown according to category ID in Custom Page; I\'m not getting a clue that How could I do?
Let me know, if you people have any solution?

1 个回复
SO网友:Andrea Somovigo

您的查询返回一个post数组(查询属于特定cat产品cat的所有产品),在您的情况下,看起来只有一个post。要在自定义页面上显示列表,必须使用以下内容迭代数组:

$args = array( \'post_type\' => \'product\', \'product_cat\' => \'category-id\' );//note that this looks like a mistake, if you want to query by id this should be an integer or an integer variable (i.e. $catid coming from somewere earlier defined) 
$query = new WP_Query($args);
$posts = $query->posts;
foreach($posts as $post) {
  // Do your stuff, e.g.
  // echo $post->post_name;
  get_template_part( \'woocommerce/single-product\');//this may or not work, it possibly need some adjustment
}

结束

相关推荐