dotif64826
dotif64826
2015-12-13 00:28

编写IF语句但没有ENDIF的正确方法?

已采纳

Am I doing this right? It works but wanted to ask if what I have done is bad practice?

Originally if the page is home then will get include a php file otherwise if it is a generic page will call in the featured image in WordPress.

<?php 
if ( is_page('home')) { 
    get_template_part( 'hero' ); 
} ?>
 <?php echo get_the_post_thumbnail($post->ID); ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanlachu7344 duanlachu7344 6年前

    Your code is wrong in my opinion based on what you requested in question. You only check if is home and if it is get template part 'hero'. After that no matter what page is you call get_the_post_thumbnail.

    otherwise if it is a generic page will call in the featured image in WordPress

    otherwise means else and your code should look like this:

    <?php 
        if ( is_page('home')) { 
            get_template_part( 'hero' ); 
        } else {
            echo get_the_post_thumbnail($post->ID);
        }
    ?>
    

    Do not forget to be in loop to call $post->ID You can aford endif by using <?php if(){ ... } ?> or <?php if(){ ?> ... <?php } ?>

    点赞 评论 复制链接分享
  • dongtuo5611 dongtuo5611 6年前

    no, it's okay. If the condition is not fulfilled, nothing will happen.

    点赞 评论 复制链接分享