6

I know there is a possibility to change the structure/design of the product page by editing the file single-product-php - in a child-theme.

The changes made of that file will affect all the product pages.

But how do I change the template file for specific product pages? Like I can with a custom page template? From scratch there is no template dropdown on a single product page like there is for a page (the image).

How do I change the template of a specific product page?

8

Woo Commerce is off topic as its a plugin and not specifically related to WordPress but what you can do is copy over the single-product.php template to a WooCommerce folder in your child theme. change the file name and modify the file, then use single_template or template_include with the correct conditional tag.

single_template

function get_custom_post_type_template($single_template) {
 global $post;

 if ($post->post_type == 'product') {
      $single_template = dirname( __FILE__ ) . '/single-template.php';
 }
 return $single_template;
}
add_filter( 'single_template', 'get_custom_post_type_template' );

template_include

add_filter( 'template_include', 'portfolio_page_template', 99 );

function portfolio_page_template( $template ) {

    if ( is_page( 'slug' )  ) {
        $new_template = locate_template( array( 'single-template.php' ) );
        if ( '' != $new_template ) {
            return $new_template ;
        }
    }

    return $template;
}
| improve this answer | |
  • None of these fully answer the question, which was "how do I change the template file for specific product pages?" – Chris J Allen Nov 8 '18 at 9:38
2

You need to check WordPress template-hierarchy how it works.

Single Post #

The single post template file is used to render a single post. WordPress uses the following path:

1.single-{post-type}.php – First, WordPress looks for a template for the specific post type. For example, post type is product, WordPress would look for single-product.php.
2.single.php – WordPress then falls back to single.php.
3.index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.

Page #

The template file used to render a static page (page post-type). Note that unlike other post-types, page is special to WordPress and uses the following patch:

   1. custom template file – The page template assigned to the page. See get_page_templates().
   2. page-{slug}.php – If the page slug is recent-news, WordPress will look to use page-recent-news.php.
   3.page-{id}.php – If the page ID is 6, WordPress will look to use page-6.php.
   4. page.php
   5. index.php

For specific id you can use page-{id}.php template.

| improve this answer | |

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