functions - I'm creating a online store for my website, my aim is to add description to products in home page only how c

Here is my codei need to add description to homepage only add_action('woocommerce_after_shop_loop_item_title'

Here is my code i need to add description to homepage only

add_action('woocommerce_after_shop_loop_item_title', 'description_in_shop_loop_item', 3 );
function description_in_shop_loop_item() {
    global $product;

    // HERE define the number of words
    $limit = 10;

    $description = $product->get_description(); // Product description
    // or
    // $description = $product->get_short_description(); // Product short description

    // Limit the words length
    if (str_word_count($description, 0) > $limit) {
        $words = str_word_count($description, 2);
        $pos = array_keys($words);
        $excerpt = substr($description, 0, $pos[$limit]) . '...';
    } else {
        $excerpt = $description;
    }

    echo '<p class="description">'.$excerpt.'</p>';
}

Here is my code i need to add description to homepage only

add_action('woocommerce_after_shop_loop_item_title', 'description_in_shop_loop_item', 3 );
function description_in_shop_loop_item() {
    global $product;

    // HERE define the number of words
    $limit = 10;

    $description = $product->get_description(); // Product description
    // or
    // $description = $product->get_short_description(); // Product short description

    // Limit the words length
    if (str_word_count($description, 0) > $limit) {
        $words = str_word_count($description, 2);
        $pos = array_keys($words);
        $excerpt = substr($description, 0, $pos[$limit]) . '...';
    } else {
        $excerpt = $description;
    }

    echo '<p class="description">'.$excerpt.'</p>';
}
Share Improve this question edited Jun 17, 2019 at 9:22 kero 6,3401 gold badge25 silver badges34 bronze badges asked Jun 17, 2019 at 9:07 ANJANA vkANJANA vk 34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use is_front_page() function in your code.

add_action('woocommerce_after_shop_loop_item_title', 'description_in_shop_loop_item', 3 );
function description_in_shop_loop_item() {
    global $product;

    if( is_front_page())
    {
        // HERE define the number of words
        $limit = 10;

        $description = $product->get_description(); // Product description
        // or
        // $description = $product->get_short_description(); // Product short description

        // Limit the words length
        if (str_word_count($description, 0) > $limit) {
            $words = str_word_count($description, 2);
            $pos = array_keys($words);
            $excerpt = substr($description, 0, $pos[$limit]) . '...';
        } else {
            $excerpt = $description;
        }

        echo '<p class="description">'.$excerpt.'</p>';
    }
}

Let me know if it is helps to you.

Thank you!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745395142a4625843.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信