2017-08-29 144 views
0

我想检查是否有post_excerpt,然后返回一些东西。我想在post_excerpt下面总是显示一些内容,甚至没有post_excerpt。我使用<?php endif; ?>,但它不起作用。 如何结束if语句?if - return - endif语句

<?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> 

我尝试使用以下:

<?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> 

我不能显示post_excerpt。

+0

究竟是在哪里,如果开始? –

+0

'endif;'关闭一个'if():'语句(在你的代码中不存在)。阅读更多关于[控制结​​构的替代语法](http://php.net/manual/en/control-structures.alternative-syntax.php)。 – axiac

回答

0

试试这个:

 <?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 }?> 
+0

它仍然不会显示..有没有其他的“有post_excerpt”段将坐在返回后面,无法访问。 – Sondre

+0

从'全球'的手' –

+0

我试过这个,它没有返回“post_excerpt” – Murasaki

2

如果你return;你阻止PHP的执行。

我想你宁愿想是这样的:

<?php 

if (! $post->post_excerpt): 

?> 

<p>Have post_excerpt</p> 

<?php endif; ?> 

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

注意,结构是这样的:

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

OR this way: 

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

您不能混用这些。

UPDATE我复制了你的代码,但我想它可能有一些问题,你确定这个$ post全局变量吗?你在用什么框架?

您应该先的var_dump($ POST)检查里面有什么了,否则你可能会想尝试:

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

仍然没有多大意义。这个块会做什么是具有post_excerpt的回声,但检查是完全相反的。猜测他真的想要删除不是运营商在if和回声如果它设置 – Sondre

+0

我试过这个,但它不能显示post_excerpt – Murasaki

+0

我添加了一个笔记给我的答案,希望有所帮助。 – Johnny