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

plugin development - How to change WooCommerce loop product title HTML output in single product page and archive page

programmeradmin0浏览0评论

I'm currently working on designing my custom WooCommerce theme and wondering how I could change the HTML output of loop_product_title in single product page and in archive pages.

I have less knowledge of functions but I made this content but it did not work. I just want the loop product title to have element and in archives pages to have .

if ( is_product() ){
function woocommerce_template_loop_product_title() {
echo '<h2 class="product-title">' . get_the_title() . '</h3>';
}
elseif ( is_shop() ){
function woocommerce_template_loop_product_title() {
echo '<span class="product-title">' . get_the_title() . '</span>';
}
}
}

Please someone help me out. I have tried the best of what I could.

I'm currently working on designing my custom WooCommerce theme and wondering how I could change the HTML output of loop_product_title in single product page and in archive pages.

I have less knowledge of functions but I made this content but it did not work. I just want the loop product title to have element and in archives pages to have .

if ( is_product() ){
function woocommerce_template_loop_product_title() {
echo '<h2 class="product-title">' . get_the_title() . '</h3>';
}
elseif ( is_shop() ){
function woocommerce_template_loop_product_title() {
echo '<span class="product-title">' . get_the_title() . '</span>';
}
}
}

Please someone help me out. I have tried the best of what I could.

Share Improve this question edited Jan 14, 2021 at 10:14 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Jan 14, 2021 at 8:10 Chotu KaaliaChotu Kaalia 31 silver badge4 bronze badges 7
  • This code has syntax errors, they're hidden because you did not indent your code, but if you do add indenting the error will be super obvious. Otherwise you can't change the start of a function definition using if else statements, if (...) { function A(){ } else { function A(){ } is not valid code and does not make sense, you can't "meta-program" like that. Maybe in a compiled language like C++ where you can use macros and a pre-processor, but not PHP. A function has to be defined in full – Tom J Nowell Commented Jan 14, 2021 at 10:14
  • Tom, could you please help with it. Like I mentioned, I am bad at php and functions. Is there a way to make the code work? – Chotu Kaalia Commented Jan 14, 2021 at 10:25
  • Indent the code and the problem becomes really really obvious, like how bullet points get indented, if you're not sure what I mean by that, see here: codehs.gitbooks.io/introcs/content/Programming-with-Karel/…, code that isn't indented is very hard for other people to read, and hides trivial mistakes that break your code – Tom J Nowell Commented Jan 14, 2021 at 10:29
  • can you please offer a correct code buddy? please! – Chotu Kaalia Commented Jan 14, 2021 at 10:56
  • edit your question to indent your code, keep in mind any answers you get here will not be copy paste snippets – Tom J Nowell Commented Jan 14, 2021 at 11:01
 |  Show 2 more comments

1 Answer 1

Reset to default 1

The main problems with what you've got are tha

  1. your close bracket } for the is_product() case is in the wrong place: it should be before the elseif not at the bottom of the block. (This is what Tom meant by indenting your code correctly: if you'd indented each {...}, which your IDE will do for you, this would have been obvious.)
  2. this code, defining the functions, will run before WordPress has enough state loaded to determine is_product() or is_shop(): at the point the theme is loaded the main query hasn't been initialised yet.

It is OK to wrap function definitions in if-else, e.g. see WordPress's pluggable.php or indeed wc-template-functions which is what you're overriding here, but that's not the correct approach for this. Instead, test is_product and is_shop inside the function:

function woocommerce_template_loop_product_title() {
    if ( is_product() ){
        echo '<h2 class="product-title">' . get_the_title() . '</h2>';
    } elseif ( is_shop() ) {
        echo '<span class="product-title">' . get_the_title() . '</span>';
    }
    // do you need an 'else' case here too?
}

because by the point this function is called WordPress will have loaded enough page state for is_product and is_shop.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论