if / else在Wordpress中测试家庭

I have an image banner in the header of my custom WP theme, on pages only (not posts). I need this banner to be different on the home page. I've created a front-page template and have this:

    <?php if (is_front_page()){ ?>
        <div class="banner">
            <?php if ( get_header_image() ) : ?>
                <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
                    <img src="<?php header_image(); ?>" width="<?php echo esc_attr( get_custom_header()->width ); ?>" height="<?php echo esc_attr( get_custom_header()->height ); ?>" alt="">
                </a>
        </div><!-- #banner -->

    <?php else { ?>
        <div class="banner">
            <?php if ( get_header_image() ) : ?>
                <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
                    <img src="<?php header_image(); ?>" width="<?php echo esc_attr( get_custom_header()->width ); ?>" height="<?php echo esc_attr( get_custom_header()->height ); ?>" alt="">
                </a>
            <?php endif; // End header image check. ?>
        </div><!-- #banner -->

I'm using the same image twice here just to test, but get a heap of errors. My php isn't great. Can anyone help?

Not much to say here, just replace you're code with this:

<?php if (is_front_page()){ ?>
    <div class="banner">
        <?php if ( get_header_image() ) : ?>
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
                <img src="<?php header_image(); ?>" width="<?php echo esc_attr( get_custom_header()->width ); ?>" height="<?php echo esc_attr( get_custom_header()->height ); ?>" alt="">
            </a>
        <?php endif; ?>
    </div><!-- #banner -->
<?php } else { ?>
    <div class="banner">
        <?php if ( get_header_image() ) : ?>
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
                <img src="<?php header_image(); ?>" width="<?php echo esc_attr( get_custom_header()->width ); ?>" height="<?php echo esc_attr( get_custom_header()->height ); ?>" alt="">
            </a>
        <?php endif; // End header image check. ?>
    </div><!-- #banner -->
<?php } ?>

To enlighten a bit:

  • You forgot a closing } before else {
  • You were missing an endif; before </div><!-- #banner -->