Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda