Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda