InsideDarkWeb.com

How to add classes to post_class?

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.

WordPress Development Asked by Antonino Lattene on November 11, 2021

2 Answers

2 Answers

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

Add your own answers!

Related Questions

Exclude category from shortcode

2  Asked on February 26, 2021 by sot

   

get_query_var to get second slash parameters

0  Asked on February 26, 2021 by v2mpire-20202

   

tax_query will produce nothing

1  Asked on February 25, 2021 by cain-nuke

 

Updating custom fields with external data

2  Asked on February 24, 2021 by hal-atkins

     

get_terms based on post meta value

0  Asked on February 22, 2021 by traummaschine

     

Sudden email notifications for new users and password changes

0  Asked on February 21, 2021 by jessica-mele

     

Breadcrumb – Getting Nav Menu Items

0  Asked on February 18, 2021 by valeron

 

Question about repurposing WordPress 404 handler

1  Asked on February 18, 2021 by yvan-gagnon

   

Set post comments open function

3  Asked on February 17, 2021

 

Woocommerce Filter orders by shipping method

0  Asked on February 16, 2021 by ssslippy

 

Make custom post type column sortable

2  Asked on February 15, 2021 by leemon

     

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.