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> </div> <a class="card-link" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"></a> </article> </div>
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); ?> <?php $classes = array( 'card', 'grid-item', 'wow', 'fadeInUp', $termsString, $custom_values ); ?> <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.
post_class function does also accept an array of classes. You can pass them to the function as follows:
$classes = [ 'card', 'grid-item', 'wow', 'fadeInUp', $termsString ]; <div <?php post_class ( $classes ); ?>> ... </div>
You can store the custom field's value inside a variable, and then pass it to the function just like any other value.
Answered by Jack Johansson on November 11, 2021
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.
Answered by swissspidy on November 11, 2021
0 Asked on March 9, 2021 by nayan-chowdhury
2 Asked on February 28, 2021 by resa-r
2 Asked on February 25, 2021 by drewdavid
2 Asked on February 24, 2021 by hal-atkins
0 Asked on February 22, 2021
0 Asked on February 22, 2021 by traummaschine
0 Asked on February 21, 2021 by jessica-mele
0 Asked on February 20, 2021 by user3344734
1 Asked on February 20, 2021 by naresh-devineni
0 Asked on February 19, 2021
1 Asked on February 18, 2021 by yvan-gagnon
0 Asked on February 16, 2021 by ssslippy
2 Asked on February 15, 2021 by leemon
Get help from others!