2017-02-12 33 views
1

我想用长描述替换产品的摘录。现在我使用下面的代码:显示说明而不是摘录,限制字词并添加阅读更多

remove_action('woocommerce_single_product_summary',  
'woocommerce_template_single_excerpt', 20); 
add_action('woocommerce_single_product_summary', 'the_content', 10); 

上面的代码完成这项工作,但它显示了完整的描述。我想以某种方式限制显示的单词(长度),并在末尾添加“阅读更多”按钮。

+2

长描述不支持阅读更多的产品。由于我们在标签上显示内容,因此没有用处。 – Yasir

回答

1

只需创建一个新的函数来处理get_the_content的值(),以获得唯一的话的最大数量,并在末尾添加了“更多”链接:

function custom_single_product_summary(){ 
    $maxWords = 50; // Change this to your preferences 
    $description = strip_tags(get_the_content()); // Remove HTML to get the plain text 
    $words = explode(' ', $description); 
    $trimmedWords = array_slice($words, 0, $maxWords); 
    $trimmedText = join(' ', $trimmedWords); 

    if(strlen($trimmedText) < strlen($description)){ 
    $trimmedText .= ' &mdash; <a href="' . get_permalink() . '">Read More</a>'; 
    } 

    echo $trimmedText; 
} 

然后在原来的使用重写代码,你试图使用:

remove_action('woocommerce_single_product_summary',  
'woocommerce_template_single_excerpt', 20); 
add_action('woocommerce_single_product_summary', 'custom_single_product_summary', 10); 

修订答: 改变了行动钩来呼应VALU因为WooCommerce期望采取行动来打印输出。

+0

感谢您的回复。但是,该产品的“摘录”部分仍未显示任何内容。 – user2093301

+0

请参阅我的更新回答。该动作需要回显输出,而不是从函数返回,以使其工作。 – ablopez