WordPress Development Asked by Antonino Lattene on November 11, 2021
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.
The 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
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP