Check if Custom Field Contains Specific Text

Great to use on product pages to reveal varied standardized text from the template when Products use shared terms like colours, or sizes.

Using Advanced Custom Fields:

<?php $colours = get_field('product_field_name');
	if (strpos($colours, 'Sizes') !== false){ } // hide content for sizes
	
	if (strpos($colours, 'Colours') !== false || strpos($colours, 'Colors') !== false ){ 

	// show content for colours

} ?>

Using get_post_meta:

<?php $colours = get_post_meta(get_the_ID,'product_field_name',true);
	if (strpos($colours, 'Sizes') !== false){ } // hide content for sizes
	
	if (strpos($colours, 'Colours') !== false || strpos($colours, 'Colors') !== false ){ 

	// show content for colours

} ?>

Source.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.