自定义摘录长度WooCommerce产品

时间:2017-02-15 作者:rhand

正在尝试使用以下方式限制WooCommerce特色产品滑块文本的摘录长度:

function custom_excerpt_length( $length ) {
    return 15;
}
add_filter( \'excerpt_length\', __NAMESPACE__ . \'\\\\custom_excerpt_length\');
我可以使用此功能将博客页面文章摘录为一定长度。它工作得很好。但是,对于WooCommerce特色产品滑块功能,我使用$productIntro = get_the_excerpt( $post ); 这不起作用。它仍然显示了整个产品的简短描述。

以下是完整功能:

function featured_products_slider()
{

    $args = array(
        \'post_type\' => \'product\',
        \'meta_key\'   => \'_featured\',
        \'meta_value\'    =>  \'yes\',
        \'post_status\' => \'publish\'
    );

    $featured   = get_posts($args);

    foreach ($featured as $post) {
        $thumb_id = get_post_thumbnail_id($post, \'featured\');
        $imageUrl = wp_get_attachment_url($thumb_id);
        $permaLink=     get_permalink($post);
        $authorID   =   $post->post_author;
        $productIntro     =   get_the_excerpt( $post );
        $title  =   $post->post_title;
        $date = get_the_date(\'M j, Y\', $post->ID);
        ?>
            <li>
                <div class="featured-post-slide">
                    <div class="post-background" style="background-image:url(\'<?=$imageUrl;?>\');"></div>
                    <div class="shadow"></div>
                    <div class="post-content">
                        <h1 class="slide-title">
                            <a href="<?=$permaLink?>"><?=$title?></a></h1>
                        <p>
                            <?= $productIntro ?>
                        </p>
                        <a href="<?=$permaLink?>" title="<?=$title?>" class="button stroke more-link">Shop now</a>
                    </div>
                </div>
            </li>
            <?php
    }
}
你知道为什么这个过滤器用于博客页面显示而不是我的特色产品滑块吗?

1 个回复
最合适的回答,由SO网友:rhand 整理而成

最后我用substr 在滑块函数中处理摘录长度。设置为从开始到140个字符。这非常有效:

function featured_products_slider()
{

    $args = array(
        \'post_type\' => \'product\',
        \'meta_key\'   => \'_featured\',
        \'meta_value\'    =>  \'yes\',
        \'post_status\' => \'publish\'
    );

    $featured   = get_posts($args);

    foreach ($featured as $post) {
        $thumb_id = get_post_thumbnail_id($post, \'featured\');
        $imageUrl = wp_get_attachment_url($thumb_id);
        $permaLink=     get_permalink($post);
        $authorID   =   $post->post_author;
        $post_excerpt     =   get_the_excerpt( $post );
        $productIntro = substr($post_excerpt, 1, 140);
        $title  =   $post->post_title;
        $date = get_the_date(\'M j, Y\', $post->ID);
        ?>
            <li>
                <div class="featured-post-slide">
                    <div class="post-background" style="background-image:url(\'<?=$imageUrl;?>\');"></div>
                    <div class="shadow"></div>
                    <div class="post-content">
                        <h1 class="slide-title">
                            <a href="<?=$permaLink?>"><?=$title?></a></h1>
                        <p>
                            <?= $productIntro ?>
                        </p>
                        <a href="<?=$permaLink?>" title="<?=$title?>" class="button stroke more-link">Shop now</a>
                    </div>
                </div>
            </li>
            <?php
    }
}

相关推荐

Excerpts on category page

有没有一种简单的方法可以让特定的分类页面显示x字数的摘录,然后通过阅读更多链接链接到完整的文章?我已经尝试了高级摘录插件,它可以满足我的需要,但我希望能够将其限制在某些类别