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

490
Visualizações
WooCommerce - determine product category of current single product page

In my header.php I want to add title based on the category of the page.

My current code looks like this:

<h1 class="page-title"><?php

    if (is_category('english') || has_category('english',$post->ID)) {

        echo "music in the world of noise";

    } elseif (is_category('marathi') || has_category('marathi',$post->ID)) {

        echo "क्षितिज जसे दिसते";

    } elseif (is_category('happenings') || has_category('happenings',$post->ID)) {

        echo "Happenings";

    } elseif (is_product() && is_product_category( 'music' ) ) {

        echo "music";

    } elseif (is_product_category( 'album' )) {

        echo "albums";

    } elseif ( is_product() && is_product_category( 'workshop' ) ) {
        echo "Workshop";
    } elseif( is_product() && has_term( 'workshop' ) ) {
        echo "Workshop";
    } else {
        the_title();
    }

    ?>

</h1>

I want to echo out Workshop in h1 if the product page is single product page AND if that product is in the workshop category. Same for Music. is_product_category works only on category page not on single product page.

How do I determine the category of single product and echo the relevant text. Other if statements (is_category('english') has_category()) are working except for the woocommerce pages?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

There is some mistakes in your code regarding WooCommerce categories:

  • Functions is_category() and has_category() doesn't work with WooCommerce product categories (WooCommerce product categories are a custom taxonomy 'product_cat').
  • Conditional is_product() && is_product_category() will not work together with &&, as is_product() will target single product pages and is_product_category() will target product categories archives pages.

To target your product categories in single product pages, you need to use Wordpress conditional function has_term() with 'product_cat' taxonomy.
You can also target product categories archives pages at the same time in your condition, if they use the same title…

So your code (for WooCommerce product categories) is going to be something like:

<h1 class="page-title"><?php

//      ... / ...

    if ( is_product() && has_term( 'music', 'product_cat' ) || is_product_category( 'music' ) ) {
        echo "Music";
    } elseif ( is_product() && has_term( 'album', 'product_cat' ) || is_product_category( 'album' ) ) {
        echo "Albums";
    } elseif( is_product() && has_term( 'workshop', 'product_cat' ) || is_product_category( 'workshop' ) ) {
        echo "Workshop";
    } else {
        the_title();
    }
?></h1>

If you don't need to target at the same time product categories archives pages, you will have to remove in each condition statement || is_product_category()…

about 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