PHP - If (condition) in wordpress

Asked

Viewed 117 times

1

Next, I have 4 posts on a post type services.

Brides, Day Spa, Aesthetic Medicine and Salon

<div class="service-content <?php if( $post_title == 'Noivas' ) echo "qwp-content"; ?>">
<?php var_dump($post_title); ?>
 <?php the_content(); ?>
</div><!-- /.qwp-content -->

CSS:
.qwp-content { 
    font-size: 1.444rem;
    text-align: center; 

    p:nth-child(odd) {
      color: #000;  
      padding: 7px;
      background: #C5C2B3; 
    }
  }

  .service-content { 
    font-size: 1.444rem;
  }

I was doing this, but I don’t get so much logic and stuff.

Basically, what I want is that when one accesses Brides, he doesn’t take the class . qwp-content and just take the . service-content

2 answers

2


In this case, you only need to change the operator.

<?php
    $post_title = "Noivas";         
?>

<div class="service-content <?php if( $post_title != 'Noivas' ) echo "qwp-content";  ?>">
    <p> teste </p>
</div>

I particularly suggest you make the condition out of the class, so you can reuse elsewhere. Ex:

<?php
        $post_title = "Não sou noiva";
        $qwp = '';
        if( $post_title != 'Noivas' ) {
            $qwp = "qwp-content";
        }
?>

<div class="service-content <?php echo $qwp ;  ?>">
    <p> teste </p>
</div>

<div class="service-main <?php echo $qwp ;  ?>">
    <p> teste </p>
</div>
  • Po guys, thank you very much, for me that I’m crawling in Wordpress and PHP, programming in general, this help is very valid, I’m lucky that I managed to do right my first little condition hehe. I did it like this and it worked: <div class="qwp-content <?php if( $post_title != 'Brides' ) echo "service-content"; ? >"> <?php the_content(); ? > </div><! -- /.qwp-content -->

0

Try to do so.

<div class="service-content <?php echo get_the_title() != 'Noivas' ? 'qwp-content' : '' ?>">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.