Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
Adding php if else statement to post_class on Wordpress posts

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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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'); } ?>">
over 4 years ago · Santiago Trujillo Report

0

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') ); ?>>
over 4 years ago · Santiago Trujillo Report

0

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()); ?>

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!