1

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 Answer 1

1

Your query returns an array of posts (query all products belonging to specific cat product cat), in your case it looks like there's ony one. To display a list on your custom page you've to iterate the array with:

$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
}

Your Answer

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

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