Hi @janvasek
The WP_Query works well on regular pages, such as the homepage. However, on WooCommerce product category pages, it might not work as effectively because WooCommerce manages queries and templates differently.
To resolve this issue, I suggest you try the following steps:
Create a new file in your theme folder and name it woocommerce.php
. This file will be used to override the default WooCommerce templates.
Copy the contents of your theme’s page.php
or index.php
file (whichever is more appropriate) into the newly created woocommerce.php
file.
Locate the main loop in the woocommerce.php
file (usually starts with if (have_posts()) : while (have_posts()) : the_post();
).
Replace the main loop with the following code:
if (is_product_category()) {
// Your custom WP_Query code
$args = array(
'post_type' => array('post'),
'posts_per_page' => 2
);
$catquery = new WP_Query($args);
while ($catquery->have_posts()) : $catquery->the_post();
get_template_part('content', get_post_format());
endwhile;
wp_reset_postdata();
} else {
// The original loop from your theme
if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile;
endif;
}
This code checks if the current page is a product category page and, if so, runs your custom WP_Query to display blog posts. Otherwise, it uses the original loop from your theme.
Save the woocommerce.php
file and refresh your product category page to see if the blog posts are now displayed correctly.
I hope this solution helps you to display blog posts on your product category pages.
Thanks!