0
votes

I'm using post_class on Wordpress blog posts to display post information in the css class like this:

<?php post_class('blog-post clearfix'); ?>

I'm also using Advanced Custom Fields to echo a css class onto each blog post using this:

<?php if (get_field('blog-post-style') == 'big-post') {
    echo('big-post');
} else if (get_field('blog-post-style') == 'small-post') {
    echo('small-post');
} ?>

So i can click a radio button option when editing a post and echo either 'big-post' or 'small-post' css class. But i also want to keep the post_class. So i need to somehow incorporate the 2 together.

I could wrap the post inside another div and echo the acf code to the containing div, but i'd rather not do that.

This is what i'd like it to look like:

<div id="post-429" class="small-post blog-post clearfix post-429 post type-post status-publish format-standard has-post-thumbnail hentry category-latest-news">

or

<div id="post-429" class="big-post blog-post clearfix post-429 post type-post status-publish format-standard has-post-thumbnail hentry category-latest-news">

Hope that makes sense. Thanks

3

3 Answers

0
votes

I worked it out using this method:

<div id="post-<?php the_ID(); ?>" class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } ?>blog-post clearfix <?php if (get_field('blog-post-style') == 'big-post') { echo('big-post'); } else if (get_field('blog-post-style') == 'small-post') { echo('small-post'); } ?>">
0
votes

You could also just pass your additional class as the first parameter of the post_class() function. It will automatically add it to the list.

<div <?php post_class( get_field('blog-post-style') ); ?>>
0
votes

One solution is wrapping the conditional statements inside a function, then passing the results to the post_class().

post_class(get_my_class())

 function get_my_class(){
    $my_class = "";
    if (get_field('blog-post-style') == 'big-post') {
        $my_class = 'big-post';
    } else if (get_field('blog-post-style') == 'small-post') {
        $my_class = 'small-post';
    }else{ 
        $my_class = 'default-class';
    }
    echo $my_class;
 }

Example of use: <?php post_class(get_my_class()); ?>