Manov Panlov
Manov Panlov

Reputation: 1

conditional statement wordpress

I want to make a conditional statement for the excerpt on my index page. If there is no manually excerpt I want the div that holds the excerpt to dispaly:none

<div class="excerpt"><?php if(!empty($post->post_excerpt)) {
     //This post have an excerpt, let's display it
     the_excerpt();
 } else {
     // <div class="excerpt" display:none?>

Upvotes: 0

Views: 341

Answers (5)

Joe Buckle
Joe Buckle

Reputation: 871

I cant think of any reasons why this shouldn't work. Unless you have a plugin that's creating manual excerpts. Sometimes search based plugins do that.

global $post
if(!empty($post->post_excerpt)) {
  the_excerpt();
} else {
  // whatever
}

Upvotes: 0

DannyTheDev
DannyTheDev

Reputation: 4173

I would use an inline if statement personally

<div class="exerpt" <?=((empty($post->post_excerpt)) ? 'style="display:none;"' : NULL)?> >
<?=the_exerpt()?>
</div>

So the html style="display:none;" only applies if empty($post->post_exerpt) is true

If you don't want to use an inline if statement you could do the following:

<?php
$style = NULL;
if(empty($post->post_exerpt()))
{
$style = ' style="display:none;" ';
}
?>
<div class="exerpt" <?=$style?> >
<?=$the_exerpt()?>
</div>

Although, why are you printing the div at all if it's just going to be hidden?

Why not:

<?php
if(!empty($post->post_exerpt()))
{
?>
<div class="exerpt">
    <?=$the_exerpt()?>
</div>
<?php
}
?>

that way the div is only added if there's content, if not, the div is ignored all together

Upvotes: 2

Jothi Kannan
Jothi Kannan

Reputation: 3358

<div class="exerpt" <?php if(empty($post->post_excerpt)) { ?> style="display:none;" <?php }?> >
<?php the_exerpt()?>
</div>

Upvotes: 0

metadings
metadings

Reputation: 3848

Just render the div differently in(line) of each case:

<?php if(!empty($post->post_excerpt)) {
    ?><div class="excerpt"><?php
    //This post have an excerpt, let's display it
    the_excerpt();
    ?></div><?php
} else {
    ?><div class="excerpt" style="display: none;" /><?php
} ?>

In this case you could also remove the else part, then it's just rendered if there is an excerpt and not even invisible.

Upvotes: 1

Ben Fortune
Ben Fortune

Reputation: 32127

<div class="exerpt" style="display:none;">

Upvotes: 0

Related Questions