默认产品简短描述
Default product short description
在我的 woocommerce 网站上,我想在我所有产品的简短描述中添加文本“仅在商店中有售”,并且我以后添加的所有产品都默认使用相同的文本。
我进行了搜索,但解决方案对我来说似乎太复杂了。我可以在 functions.php 中粘贴一些代码吗?
谢谢!
你可以轻松解决这个问题。您需要将一些代码推送到主题 functions.php 或使用 code snippets 插件。此代码仅在 WooCommerce 产品简短描述为空时有效。
function wp_woocommerce_short_description_if_empty(){
global $post;
if (empty($post->post_excerpt)) {
$post_excerpt = '<p class="default-short-desc">';
$post_excerpt .= 'Your Custom Message Here.';
$post_excerpt .= '</p>';
echo $post_excerpt;
}
}
add_action('woocommerce_single_product_summary', 'wp_woocommerce_short_description_if_empty', 21);
您也可以尝试使用此方法作为您的默认产品描述,您可以添加自定义文本 before/after 产品简短描述
add_filter( 'woocommerce_short_description', 'woo_add_text_after_excerpt_single_product', 20, 1 );
function woo_add_text_after_excerpt_single_product( $post_excerpt ){
/* Method 1: Add Custom Text before the Product Short Description on product page */
/* $content= '<ul class="fancy-bullet-points red">
<li>'.__('Only available in stores').'</li>
</ul>';
return $content.'<br>'.$post_excerpt;
*/
/* Method 2: Add Custom Text after the Product Short Description on product page */
$post_excerpt .= '<ul class="fancy-bullet-points red">
<li>'.__('Only available in stores').'</li>
</ul>';
return $post_excerpt;
}
注意:在产品页面上的产品简短描述之前添加自定义文本 - 代码已注释,因此您可以相应地取消注释。
在我的 woocommerce 网站上,我想在我所有产品的简短描述中添加文本“仅在商店中有售”,并且我以后添加的所有产品都默认使用相同的文本。
我进行了搜索,但解决方案对我来说似乎太复杂了。我可以在 functions.php 中粘贴一些代码吗?
谢谢!
你可以轻松解决这个问题。您需要将一些代码推送到主题 functions.php 或使用 code snippets 插件。此代码仅在 WooCommerce 产品简短描述为空时有效。
function wp_woocommerce_short_description_if_empty(){
global $post;
if (empty($post->post_excerpt)) {
$post_excerpt = '<p class="default-short-desc">';
$post_excerpt .= 'Your Custom Message Here.';
$post_excerpt .= '</p>';
echo $post_excerpt;
}
}
add_action('woocommerce_single_product_summary', 'wp_woocommerce_short_description_if_empty', 21);
您也可以尝试使用此方法作为您的默认产品描述,您可以添加自定义文本 before/after 产品简短描述
add_filter( 'woocommerce_short_description', 'woo_add_text_after_excerpt_single_product', 20, 1 );
function woo_add_text_after_excerpt_single_product( $post_excerpt ){
/* Method 1: Add Custom Text before the Product Short Description on product page */
/* $content= '<ul class="fancy-bullet-points red">
<li>'.__('Only available in stores').'</li>
</ul>';
return $content.'<br>'.$post_excerpt;
*/
/* Method 2: Add Custom Text after the Product Short Description on product page */
$post_excerpt .= '<ul class="fancy-bullet-points red">
<li>'.__('Only available in stores').'</li>
</ul>';
return $post_excerpt;
}
注意:在产品页面上的产品简短描述之前添加自定义文本 - 代码已注释,因此您可以相应地取消注释。