2

我正在尝试使用 WordPress 的新自定义徽标功能来完成以下任务:

  • 显示默认/备用徽标。
  • 如果 WordPress 版本支持自定义徽标,则允许用户在定制器中将默认/备用徽标替换为自定义徽标。
  • 如果 WordPress 版本不支持自定义徽标或未设置自定义徽标(或已删除),则显示默认/备用徽标。

到目前为止,这是我必须使用的最佳代码:

<?php if ( function_exists( 'the_custom_logo' ) ) : ?>
    <?php if ( has_custom_logo() ) : ?>
        <?php the_custom_logo(); ?>
    <?php else : ?> 
        <h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home" title="<?php bloginfo( 'name' ); ?>"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/logo.png" alt="<?php bloginfo( 'name' ); ?>" width="100" height="50" /></a></h1>
    <?php endif; ?>
<?php else : ?> 
    <h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home" title="<?php bloginfo( 'name' ); ?>"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/logo.png" alt="<?php bloginfo( 'name' ); ?>" width="100" height="50" /></a></h1>
<?php endif; ?>

是否有一种更清洁或更有效的方法来执行此操作,而无需重复两次后备图像的代码?

4

2 回答 2

3

谢谢,但我想我找到了更好的解决方案:

<?php if ( function_exists( 'the_custom_logo' ) && has_custom_logo() ) : ?>
    <?php the_custom_logo(); ?>
<?php else : ?> 
    <h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home" title="<?php bloginfo( 'name' ); ?>"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/logo.png" alt="<?php bloginfo( 'name' ); ?>" width="100" height="50" /></a></h1>
<?php endif; ?>
于 2016-11-16T19:07:08.950 回答
1

您可以在相同条件下测试两者function_exist()以获得单一条件。has_custom_logo()ifelse

$logo = ( ( function_exists( 'the_custom_logo' ) ) && ( has_custom_logo() ) ) ? the_custom_logo() : null;
if ($logo) {
    echo $logo;
} else {
    echo '<h1 class="site-title"><a href="' . esc_url( home_url( '/' ) ) . '" rel="home" title="' . bloginfo( 'name' ) . '"><img src="' . get_stylesheet_directory_uri() . '/images/logo.png" alt="' . bloginfo( 'name' ) . '" width="100" height="50" /></a></h1>';
}
于 2016-11-16T15:31:49.880 回答