Murasaki
Murasaki

Reputation: 23

if - return - endif statement

I want to check if have post_excerpt, then return some things. I want to display somethings below post_excerpt always, even no post_excerpt. I use <?php endif; ?>, but it is not worked. How to end the if statement?

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

global $post;

if ( ! $post->post_excerpt ) {
    return;
}

?>

    <p>Have post_excerpt</p>

<?php endif; ?>

    <p>Always display even no post_excerpt</p>

I tried to use below:

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

global $post;

if ( ! $post->post_excerpt ):
?>

<p>
    <?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?>
</p>

<?php endif; ?>

    <p>Always display even no post_excerpt</p>

I can't show the post_excerpt.

Upvotes: 0

Views: 946

Answers (2)

Johnny
Johnny

Reputation: 1798

If you return; you block the PHP execution.

I think you'd rather want something like this:

<?php

if ( ! $post->post_excerpt ):

?>

<p>Have post_excerpt</p>

<?php endif; ?>

<p>Always display even no post_excerpt</p>

Note that the construct are like this:

if (condition) {
   ...
}
else {
   ...
}

OR this way:

if (condition):
  ...
else:
  ...
endif;

You cannot mix those.

UPDATE I copied your code but I guess it might have some problems, are you sure about this $post global variable? What framework are you using?

You should first check with var_dump($post) what's in there, otherwise you might want to try:

if (!empty($_POST['post_excerpt'])):

Upvotes: 2

Azad Chouhan
Azad Chouhan

Reputation: 290

Try this:

      <?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

global $post;

if ( ! $post->post_excerpt ) {?>
        <p>Always display even no post_excerpt</p>

<?php } else{?>
  <p>Have post_excerpt</p>

    <?php }?>

Upvotes: 0

Related Questions