Support » Developing with WordPress » WooCommerce: Add category name to product title

  • hi,

    i wan’t to automatically add the name of the category-page for some products.
    the products are automatically imported, that’s why changing it per hand is not an option.

    i found the title.php, where i want to add the category name.

    <h1 class="product_title entry-title">
    	<?php echo esc_html( get_the_title() ); ?>
    </h1>

    i just can’t figure out how to display the category name.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Anonymous User 18274256

    (@anonymized-18274256)

    Hi @wrdprssnerd,

    You can get the category name with help of the below-mentioned code. You need to add it in function.php file.

    remove_action( 'woocommerce_single_product_summary','woocommerce_template_single_title', 5 );
    add_action( 'woocommerce_single_product_summary', 'custom_product_category_title', 6 );
    function custom_product_category_title(){
    	global $post;
    	$terms = get_the_terms( $post->ID, 'product_cat' );
    	$title = '';
    	foreach ($terms as $term) {
    	   $title = $term->name .' ';
    	}
    	echo "<h1 class='product_title entry-title'>".$title."</h1>";
    }

    I hope this will help you.

    Regards,

    Thread Starter wrdprssnerd

    (@wrdprssnerd)

    Hi @ravivaddweb

    at first thank you, it kinda worked, but the hook seems not correct.
    I also tried woocommerce_before_single_product and woocommerce_before_single_product_summary. It always shows the category but not the title and the normal product-title is still there.

    regards

    Thread Starter wrdprssnerd

    (@wrdprssnerd)

    Hi it worked,

    i created my own action

    add_action( 'custom_product_name_title', 'custom_product_category_title', 6 );
    function custom_product_category_title(){
    	global $post;
    	$terms = get_the_terms( $post->ID, 'product_cat' );
    	$title = '';
    	foreach ($terms as $term) {
    	   $title = $term->name .' ';
    	}
    	echo "<span>".$title."</span>";
    }

    and added the new title.php:

    <h1 class="product_title entry-title">
    	<?php do_action( 'custom_product_name_title' ); ?><?php echo esc_html( get_the_title() ); ?>
    </h1>
    Egor2015

    (@egor2015)

    @wrdprssnerd do you have a Betheme theme?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WooCommerce: Add category name to product title’ is closed to new replies.