最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Woocommerce Shop Price Position

programmeradmin1浏览0评论

I am using Storefront theme with woocommerce and I want to create a custom layout in the shop page.

This custom layout has no product thumbnail the title and price must be inside the same div so I can easily customize it with css.

However when I try to get the price inside the h2 tags it doesn't has the result I expected. It appears but above the Title and outside the custom div. What am I doing wrong?

See image for reference

remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );

add_filter( 'woocommerce_shop_loop_item_title' , 'custom_woocommerce_template_loop_product_title', 10);

function custom_woocommerce_template_loop_product_title () {
    echo '
    <div class="custom-title-wrapper">
    <h1 class="woocommerce-loop-product__title custom-loop-product-title">' . get_the_title() . '</h1>
    <h2>' . woocommerce_template_loop_price() . '</h2>
    </div>'
    ;
}

I am using Storefront theme with woocommerce and I want to create a custom layout in the shop page.

This custom layout has no product thumbnail the title and price must be inside the same div so I can easily customize it with css.

However when I try to get the price inside the h2 tags it doesn't has the result I expected. It appears but above the Title and outside the custom div. What am I doing wrong?

See image for reference

remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );

add_filter( 'woocommerce_shop_loop_item_title' , 'custom_woocommerce_template_loop_product_title', 10);

function custom_woocommerce_template_loop_product_title () {
    echo '
    <div class="custom-title-wrapper">
    <h1 class="woocommerce-loop-product__title custom-loop-product-title">' . get_the_title() . '</h1>
    <h2>' . woocommerce_template_loop_price() . '</h2>
    </div>'
    ;
}
Share Improve this question asked Apr 17, 2019 at 11:18 João TeixeiraJoão Teixeira 1087 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

It appears but above the Title and outside the custom div. What am I doing wrong?

It's because woocommerce_template_loop_price() echoes the output.

So to fix the problem, just use ?> HTML here <?php instead of echo 'HTML here';:

function custom_woocommerce_template_loop_product_title() {
    ?>
    <div class="custom-title-wrapper">
        <h1 class="woocommerce-loop-product__title custom-loop-product-title"><?php the_title(); ?></h1>
        <h2><?php woocommerce_template_loop_price(); ?></h2>
    </div>
    <?php
}

And (although this isn't a big issue,) you should use add_action() and not add_filter() because woocommerce_shop_loop_item_title is an action and not a filter:

add_action( 'woocommerce_shop_loop_item_title', 'custom_woocommerce_template_loop_product_title', 10 );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论