0

Been trying to limit the excerpt length of a WooCommerce Featured Product Slider text using:

function custom_excerpt_length( $length ) {
    return 15;
}
add_filter( 'excerpt_length', __NAMESPACE__ . '\\custom_excerpt_length');

I can make the blog page post excerpt a certain length using this function. It works just fine. However, for the WooCommerce Featured products slider function where I load text per product using $productIntro = get_the_excerpt( $post ); this does not work. It still shows the whole product short description.

Here is the full function:

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
    }
}

Any ideas why the filter is working for the blog page display and not for my featured products slider here?

0

In the end I used substr to deal with the excerpt length inside the slider function. Set is at 140 chars from the beginning. This works quite well:

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
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.