Support

Account

Home Forums Backend Issues (wp-admin) Show ACF field and title only on specific product category

Solving

Show ACF field and title only on specific product category

  • I need to show custom dates using a field titled ‘Class Dates’. I have created the ACF field but how do I add that field including the title of that field only to specific categories on woocommerce using a shortcode? And if that isn’t possible what other way is there? Any information or snippets to help would be greatly appreciated.

    Many thanks
    Matt

  • @bulldogsnare As I can understand, you have created a date field for the product category taxonomy and you want to display it in your product category archives. Here is one approach to do that –

    function tax_date_field(){
    
    	// get the current taxonomy term
    	$term = get_queried_object();
    
    	//ACF field
    	$date = get_field('class_dates', $term);
    	$class_date = '';
    
    	if( is_tax('product_cat') && $date ) {
    		$class_date = sprintf(
    			'<div class="class-date"><label>%s</label>%s</div>',
    			esc_html__('Post Dates:', 'your-themes-text-domain'),
    			esc_html( $date )
    		);
    	}
    
    	return $class_date;
    
    }

    Now you can echo the function to your product category archives. To do that, WooCommerce have some hooks for the taxonomy archive page which you can find inside archive-product.php template file inside WooCommerce plugin folder.

    Here I am hooking it to woocommerce_before_shop_loop. In your functions.php –

    add_action('woocommerce_before_shop_loop', 'show_post_dates', 11);
    function show_post_dates(){
    	echo tax_date_field();
    }

    Now the field will be shown at product taxonomy archives. Let me know if you have any questions.

  • Hi @amitbiswas06.

    Thank you for your reply. But I don’t think you’ve fully understood what I was after, apologies if I have cause any confusion.

    I have created an ACF field called class_dates which is only visible on the product category called ‘courses’.

    However, I want to give this information a title so that customers can easily see what this date is. i.e.:

    Class Dates & Time (h2 title)
    31/12/2020 at 9pm (class date field).

    Ive used the ACF shortcode to add this field to the Woocommerce product page template but I can’t pass through the title to the product page just the field text.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Show ACF field and title only on specific product category’ is closed to new replies.