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 | Show 2 more comments1 Answer
Reset to default 1The main problems with what you've got are tha
- your close bracket
}
for the is_product() case is in the wrong place: it should be before theelseif
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.) - 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.
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