問(wèn)題描述
我正在嘗試在我的描述結(jié)尾插入一些文本.可以用過(guò)濾器嗎?
I'm trying to inject some text in my description ending. Is it possible with filter?
或者我需要通過(guò)子主題來(lái)做到這一點(diǎn)嗎?一直試圖找到用于描述的鉤子,但只能找到一個(gè)用于簡(jiǎn)短描述的鉤子.示例:
Or do i need to do this via child theme? Been trying to find the hook for description but can only find one for short description. Example:
這是說(shuō)明.
只是一些示例文本來(lái)填寫說(shuō)明.
Just some sample text to fill the description out.
我想要的是注入這是描述中的最后一行"所以孔描述看起來(lái)像這樣.
What i want is to inject "This is the last line in the description" So the hole description would look like this.
這是說(shuō)明.
只是一些示例文本來(lái)填寫說(shuō)明.
Just some sample text to fill the description out.
這是描述的最后一行
我在簡(jiǎn)短描述之前注入文本的代碼是這樣的:
The code i have for injecting text before short description is this:
add_filter( 'woocommerce_short_description', 'single_product_short_descriptions', 10, 1 );
function single_product_short_descriptions( $post_excerpt ){
global $product;
if ( is_single( $product->id ) )
$post_excerpt = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>' . $post_excerpt;
return $post_excerpt;
}
推薦答案
你可以使用這個(gè)鉤在 the_content
過(guò)濾器鉤子中的自定義函數(shù):
You can use this custom function hooked in the_content
filter hook this way:
add_filter( 'the_content', 'customizing_woocommerce_description' );
function customizing_woocommerce_description( $content ) {
// Only for single product pages (woocommerce)
if ( is_product() ) {
// The custom content
$custom_content = '<p class="custom-content">' . __("This is the last line in the description", "woocommerce").'</p>';
// Inserting the custom content at the end
$content .= $custom_content;
}
return $content;
}
代碼位于活動(dòng)子主題(或活動(dòng)主題)的 functions.php 文件中.經(jīng)測(cè)試有效.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
添加 - 為空時(shí)強(qiáng)制產(chǎn)品描述(如果您希望顯示此自定義文本):
Addition - Force product description when is empty (if you want this custom text to be displayed):
add_filter( 'woocommerce_product_tabs', 'force_description_product_tabs' );
function force_description_product_tabs( $tabs ) {
$tabs['description'] = array(
'title' => __( 'Description', 'woocommerce' ),
'priority' => 10,
'callback' => 'woocommerce_product_description_tab',
);
return $tabs;
}
代碼位于活動(dòng)子主題(或活動(dòng)主題)的 function.php 文件中.經(jīng)測(cè)試有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
這篇關(guān)于將自定義內(nèi)容添加到 WooCommerce 產(chǎn)品描述的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!