Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda