Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

303
Views
How to Echo only the text with "h5" tags

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!