Woocommerce简短描述摘录长度

Rob*_*ler 2 php wordpress filter woocommerce

我正在尝试更改简短描述摘录长度.

我发现之前的帖子说我应该改变

<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
Run Code Online (Sandbox Code Playgroud)

至

<?php $excerpt = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
    echo substr($length,0, 10);
?>
Run Code Online (Sandbox Code Playgroud)

然而,当这样做时,我的摘录就会消失.

Rei*_*gel 8

我担心你正在编辑插件...如果是这样的话,你做错了..

创建一个函数,然后挂钩到那个过滤器...像这样......

add_filter('woocommerce_short_description', 'reigel_woocommerce_short_description', 10, 1);
function reigel_woocommerce_short_description($post_excerpt){
    if (!is_product()) {
        $post_excerpt = substr($post_excerpt, 0, 10);
    }
    return $post_excerpt;
}
Run Code Online (Sandbox Code Playgroud)

将其粘贴到主题的functions.php文件中.