Home › Forums › General Issues › Add Field Values to WordPress post_class
Hi,
I’m trying to get the output values from a checkbox field added to WordPress’s built in post_class short code. I may be over thinking this but can someone let me know if this is possible. Listed below are my references. Thank you.
http://codex.wordpress.org/Function_Reference/post_class
http://www.advancedcustomfields.com/resources/field-types/checkbox/
<?php
$classes = array(
'class1',
'class2',
'class3'
);
?>
<?php post_class( $classes ); ?>>
<?php echo implode(' ', get_field('field_name')); ?>
seems a little hurdled around..
<?php
$checkboxValues = get_field('field_name'); //Change field_name to your checkbox field
$extraClasses = (is_array($checkboxValues) ? implode(' ', $checkboxValues) : $checkboxValues); //If its an array, implode it! otherwise just use the value
?>
<?php post_class( $extraClasses ); //This goes in your <article> element (or whatever you use) ?>
On a second look it seems you can pass an array to post_class aswell.. so you really only need one line of code:
<?php post_class(get_field('field_name')); ?>
Just wondering. Is it possible to include multiple fields in this line of code?
It’s possible but not as is..
you’ll need to use array_merge on the fields first then put the new variable in the post_class function
Ahh I used to be one too, then I became both after a while 😉 It’s inevitable in a small business
Anyway, this should do it provided that both fields are arrays!
<?php
$checkboxValues = get_field('field_name'); //Change field_name to your checkbox field
$morecheckboxValues = get_field('field_name_2'); //Change field_name to your checkbox field
$extraClasses = array_merge($checkboxValues, $morecheckboxValues);
?>
<?php post_class( $extraClasses ); //This goes in your <article> element (or whatever you use) ?>
Thanks man. I really appreciate the time you took to help me. Best wishes.
The topic ‘Add Field Values to WordPress post_class’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.