【问题标题】:Trim product name on WooCommerce product loop在 WooCommerce 产品循环上修剪产品名称
【发布时间】:2019-07-27 21:52:38
【问题描述】:

我想在商店页面或有多个产品的任何地方(如主页产品滑块等)修剪产品标题。 现在,我有这个代码

<?php
function shorten_woo_product_title($title, $id) {
if (!is_product()) {
$title = wp_trim_words($title, 7);
return $title;
}
else {
return $title;
}
add_filter('the_title', 'shorten_woo_product_title', 10, 2); ?>

这工作得很好,但它会修剪我在主页上显示的帖子,这会使我的网站无法加载博客页面。我只想修剪产品标题而不是发布标题。我还想在单个产品页面内的相关产品部分修剪产品标题。 仅供参考:此代码已添加到 functions.php 文件中。

【问题讨论】:

    标签: php wordpress woocommerce product


    【解决方案1】:
    function shorten_woo_product_title($title, $id) {
        if (is_shop() || is_product_category() || is_product_tag() || is_home() || is_front_page()) {
    
            if (get_post_type($id) == 'product') {
    
                $title = wp_trim_words($title, 7);
            }
        }
    
        return $title;
    }
    
    add_filter('the_title', 'shorten_woo_product_title', 10, 2);
    

    【讨论】:

    • 谢谢,但它一直给我的博客页面造成问题。此外,它不会修剪单个产品页面中相关产品部分的标题,并且在我显示一些最新产品的主页上也不起作用。
    • 您使用的是哪个主题?
    • 这里叫第五大道。
    【解决方案2】:

    我使用此代码并将字符数更改为 40,但随后进入第 2 行,然后添加到购物车按钮的对齐受到干扰,它与其他产品对齐发生了变化。

    add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
    function shorten_woo_product_title( $title, $id ) {
        if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) {
            return substr( $title, 0, 30) . '…'; // change last number to the number of characters you want
        } else {
            return $title;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      相关资源
      最近更新 更多