2017-10-09 15 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); 

そして、このコードはショップページに正常に動作しますが、私は「おすすめの製品」や「最近の製品」ウィジェットオム私のスタートページを使用する場合、それは動作しません。 ウィジェットでも動作するようにこのコードを変更するにはどうすればよいですか?

私の英語のために申し訳ありません。

答えて

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ですが、それは私のために働いていません。 –

関連する問題