dongyang1518 2014-01-19 15:00
浏览 22

如何使用WooCommerce截断产品描述中的字符

I have found out how to add a short description to the thumbnails for products in WooCommerce, but how do I truncate them to a certain length, say 30 characters. All the answers to do with editing the functions.php file dont mention where in the file to put the code.

My code in my functions.php file is:

add_action('woocommerce_after_shop_loop_item_title','add_title_description',9);
function add_title_description(){
  echo get_post_meta($product->id, 'title-description', true)? '<span class="title-description">' . get_post_meta($product->id, 'title-description', true) . '</span><br />' : '';
}
  • 写回答

2条回答 默认 最新

  • douye4051 2014-01-19 15:06
    关注

    use substr()

    add_action('woocommerce_after_shop_loop_item_title','add_title_description',9);
    
    function add_title_description()
    {
        $titleDescription = get_post_meta($product->id, 'title-description', true);
    
        if( !empty($titleDescription) )
        {
            if( strlen($titleDescription) > 30 )
                $titleDescription = substr($titleDescription, 30);
    
                printf('<span class="title-description">%s</span><br />', $titleDescription);
            }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题