I'm trying to echo text with special tags "h5" So I added this code inside function.php to echo the short description under each product
add_action( 'woocommerce_after_shop_loop_item', 'woo_show_excerpt_shop_page', 5 );
function woo_show_excerpt_shop_page() {
global $product;
echo $product->post->post_excerpt;
}
It works but it echo all the text
So I edited the code to only pick up h5 tags
add_action( 'woocommerce_after_shop_loop_item', 'woo_show_excerpt_shop_page', 5 );
function woo_show_excerpt_shop_page() {
global $product;
//pick up only h5
$html = '<h5></h5>';
$dom = new DOMDocument();
$dom->loadHTML( $html );
echo $dom->getElementsByTagName("h5")->item(0)->textContent;
}
Now my problem is the text doesn't show up at all! What I'm doing wrong?
Remember that computers are stupid, they don't know what you mean, only what you say. So in this code:
$html = '<h5></h5>';
$dom = new DOMDocument();
$dom->loadHTML( $html );
echo $dom->getElementsByTagName("h5")->item(0)->textContent;
The computer will load the string '<h5></h5>'
, parse it as HTML, and then extract h5
elements from it. At no point will it look at any other text.
What you want to do is look for h5
elements inside another string, so you need to tell the computer what other string. Based on your first attempt, the string you want to look in is $product->post->post_excerpt
, so that is the value you need in $html
:
global $product;
$html = $product->post->post_excerpt;
$dom = new DOMDocument();
$dom->loadHTML( $html );
echo $dom->getElementsByTagName("h5")->item(0)->textContent;