PHP WordPress如果没有工作意外的语法

时间:2014-04-22 15:24:52

标签: php wordpress if-statement

我有一个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 
    }
?> 

这是错误,我得到语法错误,意外&#39;)&#39;

我不确定意外的来自哪里。

我将PHP基于此结构,但编辑它,因为我希望能够将我的HTML放入HTML而不使用echo

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

4 个答案:

答案 0 :(得分: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; ?>

答案 1 :(得分:2)

我不确定为什么你用这种方式编写你的代码而不是连接它,但你的if else语句在“()”之后有两个括号。你可能想要摆脱这些来解决你的问题。

答案 2 :(得分:0)

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

答案 3 :(得分: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用户将无法理解您的问题/帮助您进一步。