In the following code I would like to remove the div that wrap all the code and pass its class to article, but I do not know how to pass the variable $termString inside the post_class.

Can anybody help me?

<div class="<?php echo $termsString;?>">
    <article id="post-<?php the_ID(); ?>" <?php post_class('card'); ?>>
        <?php echo get_the_post_thumbnail($post_id, 'large', array('class' => 'img-fluid card-img-top')); ?>
        <div class="overlay"></div>
        <div class="card-body text-right">
            <h6 class="card-title"><?php the_title(); ?></h6>
            <p>Text description for this item</p>
        <a class="card-link" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"></a>  

So, I have this now, and it is what I expected:

<article id="post-<?php the_ID(); ?>" <?php post_class('card grid-item wow fadeInUp ' . $termsString); ?>>

but I also need to add to these classes another class that comes from a custom field which name is "columns" and with value "col-12".

This is what I am trying, but I think there's some syntax error, the result I see from the Firefox inspector is "Array" instead of the value of "columns":

<?php $custom_values = get_post_meta($post->ID, 'columns', true); ?>
                        $classes = array(
                <article id="post-<?php the_ID(); ?>" <?php post_class( $classes ); ?>>

Edit: get_post_meta requires a third parameter, "false" returns the Array (default), "true" returns only the first result (NOT as an array). Now it is working! Thanks a lot.

  • 1
    You may want to wrap $termsString in the esc_attr function for security reasons, that way it's escaped – Tom J Nowell Jan 27 '18 at 1:51
  • The post_class() function already runs esc_attr() on all elements AFAIK. – swissspidy Jan 27 '18 at 10:36

The post_class function does also accept an array of classes. You can pass them to the function as follows:

$classes = [

<div <?php post_class ( $classes ); ?>>

You can store the custom field's value inside a variable, and then pass it to the function just like any other value.

| improve this answer | |
  • Something like this? <?php $custom_values = get_post_meta($post->ID, 'columns'); ?> $classes = [ 'card', 'grid-item', 'wow', 'fadeInUp', $termsString, $custom_values ]; <div <?php post_class ( $classes ); ?>> ... </div> – Antonino Lattene Jan 27 '18 at 1:04
  • Yes, that's exactly how. – Jack Johansson Jan 27 '18 at 1:53
  • It is not working, there is some error that I can not detect, I have edited the question. – Antonino Lattene Jan 27 '18 at 10:58

You should be able to just use post_class( 'card ' . $termString ).

The function accepts arrays and strings, see https://developer.wordpress.org/reference/functions/post_class/

You can also use the post_class filter to add more classes if you prefer that way.

| improve this answer | |
  • Thank you, I forgot to put the space after "card". How can I include my custom field without using filters? – Antonino Lattene Jan 26 '18 at 23:41
  • Using your custom meta value as the post class? That works the same way. An example post_class( get_post_meta( get_the_ID(), 'my_meta_key', true ) ). If that‘s not what you meant, please update your question accordingly :-) – swissspidy Jan 26 '18 at 23:44
  • I just edited it. – Antonino Lattene Jan 26 '18 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.