【问题标题】:Woocommerce product description length charactersWoocommerce 产品描述长度字符
【发布时间】:2014-04-14 15:12:06
【问题描述】:

我将以下代码添加到我的 functions.php 文件中,以便在我的商店页面的缩略图下添加简短的产品描述。

add_action( 'woocommerce_after_shop_loop_item_title', 'lk_woocommerce_product_excerpt', 35, 2);
if (!function_exists('lk_woocommerce_product_excerpt'))
{
function lk_woocommerce_product_excerpt()
{
$content_length = 20;
global $post;
$content = $post->post_excerpt;
$wordarray = explode(' ', $content, $content_length + 1);
if(count($wordarray) > $content_length) :
array_pop($wordarray);
array_push($wordarray, '...');
$content = implode(' ', $wordarray);
$content = force_balance_tags($content);
endif;
echo "<span class='excerpt'><p>$content</p></span>";
}
}

正如您在我的网站上看到的here,描述在那里,但长度基于单词。无论如何,我可以根据字符而不是单词来确定描述的内容长度吗?

【问题讨论】:

    标签: php wordpress function woocommerce


    【解决方案1】:

    您可以尝试使用substr() 方法: 例如

    $content = substr($content, 0, 20);
    

    其中20 是您要返回的字符数。

    代码:

    add_action( 'woocommerce_after_shop_loop_item_title', 'lk_woocommerce_product_excerpt', 35, 2);
    if (!function_exists('lk_woocommerce_product_excerpt'))
    {
    function lk_woocommerce_product_excerpt()
    {
    $content_length = 20;
    global $post;
    $content = $post->post_excerpt;
    $wordarray = explode(' ', $content, $content_length + 1);
    if(count($wordarray) > $content_length) :
    array_pop($wordarray);
    array_push($wordarray, '...');
    $content = implode(' ', $wordarray);
    $content = force_balance_tags($content);
    $content = substr($content, 0, 20);
    
    endif;
    echo "<span class='excerpt'><p>$content</p></span>";
    }
    }
    

    【讨论】:

    • 太棒了,效果很好!现在,正如您在我的 [lostgrok.com](site) 中看到的那样,描述夹在价格和按钮之间。我怎样才能像以前一样在顶部进行描述?非常感谢!!
    • clear:both; 添加到excerpt 跨度中的段落标签应该可以解决这个问题。
    • 非常感谢!那肯定固定了三明治,我怎样才能使文本显示在价格和按钮上方?当我将钩子从 (35,2) 更改为 (5,2) 时,它会推动上面的 desc,但会采用标题的样式。希望这是有道理的.. see problem here
    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2015-11-08
    • 2018-07-27
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    相关资源
    最近更新 更多