精选图像的IF语句

时间:2015-04-08 作者:kia4567

我只是想确保我的主页和页面显示我的特色图片,如果设置了,其他明智的,如果没有设置显示博客横幅。jpg。所以这就是我认为我有,但现在我的404错误页面没有横幅图像。关于更正此if语句,有什么建议吗?

/** Add the featured image section */
add_action( \'genesis_before_header\', \'minimum_featured_image\' );
function minimum_featured_image() {

global $post; 
$post->ID;

if ( is_home() ) {
    echo \'<div id="featured-image-home">\';
    echo get_the_post_thumbnail($thumbnail->ID, \'header\');
    echo \'</div>\';
}
if ( is_singular( array( \'post\' ) ) && ( !has_post_thumbnail( $post->ID ) ) ){
    echo \'<div id="featured-image-home"><img src="\'. get_stylesheet_directory_uri() . \'/images/blog-banner.jpg" /></div>\';
    return;
}
if ( is_singular( array( \'post\', \'page\' ) ) && has_post_thumbnail( $post->ID ) ){
    echo \'<div id="featured-image">\';
    echo get_the_post_thumbnail($thumbnail->ID, \'header\');
    echo \'</div>\';
}
}

2 个回复
SO网友:jetlej

第二个if语句要求它是一篇文章,因此404页无法通过。

我会这样做,使用elseif语句。我还将$缩略图->ID更改为$post->ID,因为前者没有定义。

add_action( \'genesis_before_header\', \'minimum_featured_image\' );
function minimum_featured_image() {

    global $post; 

    if ( is_home() ) {
        echo \'<div id="featured-image-home">\';
        echo get_the_post_thumbnail($post->ID, \'header\');
        echo \'</div>\';
    }
    elseif ( is_singular( array( \'post\' , \'page\' ) ) && ( !has_post_thumbnail( $post->ID ) ) ){
        echo \'<div id="featured-image">\';
        echo get_the_post_thumbnail($post->ID, \'header\');
        echo \'</div>\';
    }   
    elseif(is_singular( array( \'post\' , \'page\' ) ){
        echo \'<div id="featured-image-home"><img src="\'. get_stylesheet_directory_uri() . \'/images/blog-banner.jpg" /></div>\';
    }
} 

SO网友:vadims

你还需要一个条件

if ( is_404() ) {
    // your code here
}
您的职能:

add_action( \'genesis_before_header\', \'minimum_featured_image\' );
function minimum_featured_image() {

global $post; 

if ( is_home() ) {
    echo \'<div id="featured-image-home">\';
    echo get_the_post_thumbnail($post->ID, \'header\');
    echo \'</div>\';
}
elseif ( is_singular( array( \'post\' , \'page\' ) ) && ( !has_post_thumbnail( $post->ID ) ) ){
    echo \'<div id="featured-image">\';
    echo get_the_post_thumbnail($post->ID, \'header\');
    echo \'</div>\';
}   
elseif( is_singular( array( \'post\' , \'page\' ) ) ) {
    echo \'<div id="featured-image-home"><img src="\'. get_stylesheet_directory_uri() . \'/images/blog-banner.jpg" /></div>\';
}
elseif( is_404() ) {
    echo \'<div id="featured-image-home"><img src="\'. get_stylesheet_directory_uri() . \'/images/blog-banner.jpg" /></div>\';
}
}

您可能还需要检查conditional tags

结束

相关推荐