Reputation: 3285
I am using ACF and have set up a field for my products called product_title
.
I am trying to display this custom field in on my product page using the WooCommerce hooks like this in my functions.php file.
function hom_add_product_header()
{
$product_title = the_field('product_title');
?>
<h1><?php echo $product_title; ?></h1>
<?php
}
add_filter('woocommerce_single_product_summary', 'hom_add_product_header', 5);
But when I look at the rendered page the product_title
is rendered outside of empty <h1></h1>
tags!
I have also tried output buffering like this...
function hom_add_product_header()
{
# Add the start
ob_start();
$product_title = the_field('product_title');
?>
<h1><?php echo $product_title; ?></h1>
<?php
# Fetch the cache
$data = ob_get_contents();
# End and clear the buffer
ob_end_clean();
# Send data back for render
return $data;
}
add_filter('woocommerce_single_product_summary', 'hom_add_product_header', 5);
But with this code nothing gets displayed at all!
Why is my content not being rendered inside the HTML tags?
Upvotes: 0
Views: 1342
Reputation: 1120
This should work:
function hom_add_product_header()
{
$product_title = get_field('product_title');
echo "<h1>" . $product_title . "</h1>"
}
The php script is parsed from top to bottom and each non php part is directly rendered on parsing. Since you are calling you function a few lines after the declaration the parser sees the <h1></h1>
tags and renders them. After that the renderer gets to the function call an executes the function. The critical part is that you are breaking the php controll flow within the function.
Upvotes: 1