2016-02-29 56 views
2

我正在尝试更改简短摘要摘录长度。Woocommerce简短摘要长度

我发现了一个以前的帖子,指出这样做,我摘录完全消失时,我应该改变

<?php echo apply_filters('woocommerce_short_description', $post->post_excerpt) ?> 

<?php $excerpt = apply_filters('woocommerce_short_description', $post->post_excerpt); 
    echo substr($length,0, 10); 
?> 

不过。

+2

仔细看看你的变量名称,你存储你的摘录和'substr'的​​第一个参数。看到一些区别? :P –

回答

4

恐怕你正在编辑的插件......如果可以的话,你就错了..

创建一个函数,然后勾到过滤器......这样的事情.. 。

add_filter('woocommerce_short_description', 'reigel_woocommerce_short_description', 10, 1); 
function reigel_woocommerce_short_description($post_excerpt){ 
    if (!is_product()) { 
     $post_excerpt = substr($post_excerpt, 0, 10); 
    } 
    return $post_excerpt; 
} 

将其粘贴到您主题的functions.php文件中。

+0

<?php echo apply_filters('woocommerce_short_description',$ post-> post_excerpt); ?>位于“content-product.php”中,位于我的主题中。 恐怕这个功能创造了同样的问题,我的摘录消失了。 –

+0

是的..试图添加我的代码,而不编辑'product.php' ... – Reigel

+0

这是正确的,内容没有改变,但这个功能不工作,并与原来的<?php echo apply_filters( 'woocommerce_short_description',$ post-> post_excerpt); ?>。 –

2

不是直接在插件中编辑文件(这是因为一旦更新插件和所有的修改都将丢失一个非常糟糕的主意!)

您可以使用此代码限制无话的 -

add_filter('woocommerce_short_description', 'limit_woocommerce_short_description', 10, 1); 
    function limit_woocommerce_short_description($post_excerpt){ 
     if (!is_product()) { 
      $pieces = explode(" ", $post_excerpt); 
      $post_excerpt = implode(" ", array_splice($pieces, 0, 20)); 

     } 
     return $post_excerpt; 
    } 

爆炸休息原字符串成字的数组,array_splice让你得到的那些话一定范围内,然后破灭结合的范围重新走到一起成于思单独的字符串。

使用此代码可以改变商店页面上的限制而不是产品详细页面

+0

有什么办法可以在摘录结尾处放置...(三个可乐)来表明它没有被完全引用,但是被切断了吗?谢谢。 – Elijah