2017-10-09 75 views
0

我想限制產品說明的長度。限制「特色產品」或「最近產品」中的產品說明長度

function short_product_titles_chars($title, $id) { 
    if (
     (is_shop() || is_product_tag() || is_product_category()) && 
     get_post_type($id) === 'product') 
    { 
     if (strlen($title) >= 25) { 
      return substr($title, 0, 25) . '...'; 
     } else { 
      return $title; 
     } 
    } else { 
     return $title; 
    } 
} 
add_filter('the_title', 'short_product_titles_chars', 10, 2); 

而這個代碼工作的店鋪頁面上的罰款,但如果我使用了「特色產品」或「最近的產品」窗口小部件OM我的起始頁這是行不通的。 如何更改此代碼,使其甚至可以與小部件一起使用?

對不起,我的英語

回答

0

請試試這個。

add_action('woocommerce_after_shop_loop_item_title', 'product_excerpt', 35, 2); 

function product_excerpt() 
{ 
    $length = 30; 
    global $post; 
    $content = $post->post_excerpt; 
    $wordarray = explode(' ', $content, $length + 1); 
    if(count($wordarray) > $length) : 
     array_pop($wordarray); 
     array_push($wordarray, '...'); 
     $content = implode(' ', $wordarray); 
     $content = force_balance_tags($content); 
     $content = substr($content, 0, 30); 
    endif; 
    echo "<p>".$content."</p>"; 
} 
+0

嗨,thx爲您的答案,但它沒有爲我工作。 –