wordpress add field to post_class
-
09-12-2019 - |
Question
I need to add a custom field to my article post but not sure how to add an additional class to it.
Currently the classes get pulled through like this <?php post_class($classes); ?>
.
However I need to add a custom field to this as well. To demonstrate ive added a class= but this doesnt work as class= is being added twice.
<?php post_class($classes); ?> class="<?php the_field( "size" ); ?>
So i need post_class and the_field to work together.
Solution
I would first store your field value in a variable after your loop just before the posts HTML tags start:
<?php
$size = the_field( "size" );
?>
And then add this variable to the post class function:
<?php post_class($size); ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange