posts – Separate top level categories on archive-product.php by meta

Question

I’m trying to edit the top-level products/shop page for WooCommerce, which I know is archive-product.php. I only want the changes I’m making to affect the main level so I’m using an ‘if’ statement to check if is_shop() is true. Inside this statement I want to use the same start & and wrappers to the loop, which I managed to pull out from their respective functions. All of this works fine.

The issue is that, using the typical while (have_posts()): the_post(); is showing all product posts instead of the top level categories like normal, even though it appears to use the same code?

What I’d like to do is separate the top level product categories into groups by a custom meta field associated with the category (which has already been added and works). I can’t even get the regular categories to show up now, let alone start sorting them and I’m at a loss 🙁

// top level shop loop
if ( is_shop() ) {
    wc_get_template( 'loop/loop-start.php' );

    // This is where I'm having trouble
    while ( have_posts() ) {
        the_post();
        do_action( 'woocommerce_shop_loop' );
        wc_get_template_part( 'content', 'product' );
    }

    wc_get_template( 'loop/loop-end.php' );
}
// subcategories (this part work fine, as it's the original code)
else {
    woocommerce_product_loop_start();
    if ( wc_get_loop_prop( 'total' ) ) {
        while ( have_posts() ) {
            the_post();
            do_action( 'woocommerce_shop_loop' );
            wc_get_template_part( 'content', 'product' );
        }
    }
    woocommerce_product_loop_end();
}

0
AlexM 1 month 2021-12-16T18:01:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse