winfred adrah
winfred adrah

Reputation: 428

Edit Single Product Page for A Specific Product

I want to re-arrange the elements (price, wishlist button and others) on the single product page for only specific products. The reason is, the client i am working for sells Gift Vouchers and needs the arrangement for the Gift Vouchers single product page to be different from regular products. The gift vouchers are just about 4 so i don't mind doing this for each of them or if possible do it for the category they belong to.

I have looked online but the answers i find apply to all products rather than specific ones.

Thank you in advance for your help.

Upvotes: 1

Views: 178

Answers (1)

berend
berend

Reputation: 553

First you need to obviously create a template file for those 4 products, which is different from the default single-product template.

If you have done that you can use the following code to point them to that template file:

function get_custom_post_type_template($single_template) {
 global $post;

 if ($post->post_type == 'product') {
      $single_template = 'location_of_your_template_file';
 }
 return $single_template;
}
add_filter( 'single_template', 'get_custom_post_type_template' );

Now obviously the above code works for all products, you could for example change the if statement to manually check for certain post ID's:

$post_ids = array(1,2,3,4,5);
 if ($post->post_type == 'product' && in_array($post->ID, $post_ids) {
      $single_template = 'location_of_your_template_file';
 }

Or, better in my opinion check for a certain category:

if ($post->post_type == 'product' && has_category("category_name_here", $post->ID) {
      $single_template = 'location_of_your_template_file';
 }

Whichever you prefer, hope it helps.

Upvotes: 1

Related Questions