2014-04-22 38 views
0

我有一個PHP if else聲明,但它似乎並沒有工作,我不知道爲什麼。PHP的WordPress如果不工作意外的語法

這裏是我的代碼

<?php 
    if (has_post_thumbnail()) { 
?> 
     <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" > 
<?php 
     the_post_thumbnail('full', array('class' => 'img-responsive alignleft')); 
?> 
</a> 
<?php 
    } else() { 
?> 
     <img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />'; 
<?php 
    } 
?> 

這裏是我得到語法錯誤,意想不到的,我不知道在哪裏意外)是來自錯誤「)」

我在此基礎上構造我的PHP,但是編輯它,因爲我希望能夠把我的成HTML,而不使用echo

<?php if ('' != get_the_post_thumbnail()) { 
// some code 
} 
else { 
// some code 
} 
?> 
+3

我很確定在PHP中不允許有空'elseif'。 else()都不是。 – NorthBridge

回答

3

讓我們試試這個:

<?php if (has_post_thumbnail()): ?> 
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" > 
     <?php the_post_thumbnail('full', array('class' => 'img-responsive alignleft')); ?> 
    </a> 
<?php else: ?> 
    <img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />'; 
<?php endif; ?> 
2

我不確定爲什麼你這樣寫代碼而不是連接它,但是你的else語句在else(「)之後有兩個括號。您可能想要擺脫這些問題來解決您的問題。

0

在PHP 其他不是一個函數,所以你不應該這樣稱呼它。

0

PHP的elseif() needs conditions
嘗試else代替:

<?php 
    if (has_post_thumbnail()) { 
?> 
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('full', array('class' => 'img-responsive alignleft')); ?></a> 
<?php 
    } else { //instead of elseif() here, use else 
?> 
    <img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" /> 
<?php 
    } 
?> 

另請參閱WordPress Coding Standards,使您的代碼更容易閱讀和維護。並且儘量不要在你的問題中編輯原始代碼而不清楚標記編輯,否則SO用戶將無法理解你的問題/進一步幫助你。