• <?php 
            $args = array( 
                'post_type' => array('post'),
                'posts_per_page' => 2 
            );
            $catquery = new WP_Query( $args ); ?>
                                     
                <?php while($catquery->have_posts()) : $catquery->the_post(); ?>
                
                <?php get_template_part( 'content', get_post_format() );?>
                
                <?php endwhile; wp_reset_postdata(); ?>

    Hello, i have this wp-query – working, just only on woocommerce pages not. When i’m on classic page, homepage etc.. i get blog posts, but on product-category page i cant get blog posts.

    Can anybody help, please? Thank you Jan

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    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!

    Hi @janvasek

    Thanks for reaching out!

    I understand that you are using the code snippet above that works on classic pages, however, it is not on WooCommerce pages, correct?

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

    Thread Starter janvasek

    (@janvasek)

    Hi @shameemreza

    thank you, but i need on the product-category page show products and blog posts at the same time. For example i have product-category Apple phones and i want show blog posts with apple phones category.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Blog posts on woocommerce pages (product-category) not working’ is closed to new replies.