PHP WordPress如果其他不工作意外的语法


PHP WordPress if else not working unexpected syntax

我有一个PHP if 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 () {
?>
        <img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php 
    }
?> 

这是我得到的错误语法错误,意外的')'

我不确定意外的)是从哪里来的。

我的PHP是基于这个结构的,但是编辑它,因为我希望能够在不使用echo 的情况下将我的PHP放入HTML中

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

让我们试试这个:

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

我不知道为什么用这种方式编写代码,而不是将其连接起来,但if-else语句在else"()"后面有两个括号。你可能想摆脱这些来解决你的问题。

在PHP中,else不是一个函数,所以不应该像以前那样调用它。

PHP的elseif ()需要条件,
请尝试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编码标准,使您的代码更易于阅读和维护。在没有清楚标记编辑内容的情况下,尽量不要编辑问题中的原始代码,否则SO用户将无法理解您的问题/进一步帮助您。