I have a number of products that are added to each post and I need a list of those products as classes for each post.
First the code:
<li class="<?php the_field( 'products' ); ?>"></li>
Now the output I’m getting:
<li class="Product One, Product Two, Product Three"></li>
Desired output:
<li class="product-one product-two product-three"></li>
Any help is appreciated, thanks!
Hi @cmegown,
What type of field is ‘products’?
Cheers
Hi @cmegown
You can render the data corerectly like so:
<?php
$products = get_field( 'products' );
$products = array_map('sanitize_title', $products);
$products = implode(' ', $products );
?>
<li class="<?php echo $products; ?>"></li>
Links:
http://codex.wordpress.org/Function_Reference/sanitize_title
http://au2.php.net/array_map
@elliot your solution works perfectly, and it totally just saved my bacon. Thanks also for the links – I’ll keep those for further education!
When I try to use this solution, I get PHP warnings:
PHP Warning: implode() [function.implode]: Invalid arguments passed in /Applications/MAMP/htdocs/wp-content/themes/theme2016/page-our-team.php on line 127
What do I need to adjust?
$team = get_field('team');
$team = array_map('sanitize_title', $team);
$team = implode(' ', $team );
team field is a Checkbox in a custom post type if it matters.