Hello. Suppose a certain template is assigned to the main store page in the wordpress admin, i.e. in which there is a Template Name: The name of the template. In the file content-product.php, which is called by default for all pages of the store in the loop, I do a check:

 $ classes = '';
 if (is_page_template ('content-main.php')) {
     $ woocommerce_loop ['loop'] ++;
     if ($ woocommerce_loop ['name'] == 'product_cat') {?>
        

But this check does not work, i.e. The standard code in li tags is called. And I need to wrap the goods in other blocks, depending on the page and shortcode. The content-main.php file in this case is the template for the main page.

    1 answer 1

    is_page_template() does not work inside a WordPress loop.

    Most likely, the point is this and the test must be moved out of the cycle.

    You can try to figure out the template used in an alternative way, through the page's meta-fields:

     if ( get_page_template_slug( get_the_ID() ) == 'content-main.php' ) {} 

    The second method should work inside the loop.

    UPDATE

    The second method works for a cycle through pages, and WooCommerce performs its cycle by product, and get_the_ID() does not work here. In the case of WooCommerce, you can determine which template to use as follows.

    In functions.php add a filter that tracks the change of the template and stores it in a global variable, as well as a function that returns the value of this global variable.

     function filter_template_include( $t ) { $GLOBALS['current_template'] = basename($t); return $t; } add_filter( 'template_include', 'filter_template_include', 1000 ); function get_current_template() { if( !isset( $GLOBALS['current_template'] ) ) return false; return $GLOBALS['current_template']; } 

    In the content-product.php file, use this function to get the name of the current template.

     if (get_current_template() == 'content-main.php') {} 

    I remind you that it is better to copy content-product.php to the subfolder of your theme /wp-content/themes/my-theme/woocommerce/ and edit there. So it will remain when updating WooCommerce.

    For the test page was created on the test site. She is assigned a woo-test template. Contents of the file so632379.php

     <?php /* Template Name: woo-test */ echo do_shortcode('[products]'); ?> 

    In the content-product.php added line output template name

     // Ensure visibility if ( empty( $product ) || ! $product->is_visible() ) { return; } echo "Current template = " . get_current_template(); ?> <li <?php post_class(); ?>> 

    The result is here .

    enter image description here

    • How to move out of the loop if only standard template content-product.php is called in woocommerce plugin? <? php wc_get_template_part ('content', 'product'); ?> After all, only in this file content-product.php it turns out you can do a check and find out where this php file came from. Tell me how to make a check for the cycle? It turns out you need to override the method woocommerce_content () - word
    • The second method should work inside the loop. - word
    • if (get_page_template_slug (get_the_ID ()) == 'content-main.php') {} no, does not work - word
    • Yes. Found out why and added the answer. - KAGG Design