2017-08-29 146 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