Product shortcode - Removing product title on blog posts and pages

情到浓时终转凉″ 提交于 2021-01-28 01:10:11

问题


How do I remove the title on WooCommerce products when they are posted as short codes?

[product_page id="99"]

I am getting a double title:

  1. on the shortcode product embed
  2. on the actual blog post

I would like to disable to title on the shortcode only and keep the title on the blog post, but keeping the title on the shop page.


回答1:


Those titles are hooked in content_single-product.php WooCommerce template file as you can see below:

/**
 * woocommerce_single_product_summary hook.
 *
 * @hooked woocommerce_template_single_title - 5  // <===  HERE
 * @hooked woocommerce_template_single_rating - 10
 * @hooked woocommerce_template_single_price - 10
 * @hooked woocommerce_template_single_excerpt - 20
 * @hooked woocommerce_template_single_add_to_cart - 30
 * @hooked woocommerce_template_single_meta - 40
 * @hooked woocommerce_template_single_sharing - 50
 */

To remove the product page titles only from your blog posts and pages, when outputted from a shortcode, you will need to add a condition when removing the title in the woocommerce_single_product_summary hook.

Here is that functional and tested code:

function remove_some_product_titles(){
    if( !is_product() ){
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5);
    }
}
add_action( 'woocommerce_single_product_summary', 'remove_some_product_titles', 4);

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.




回答2:


You can remove the title action using this:

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );

Let me know if it worked.



来源:https://stackoverflow.com/questions/40955866/product-shortcode-removing-product-title-on-blog-posts-and-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!