我正在将标题图像设置为.blog__header
通过函数中的内联样式阻止。php:
$header_image = get_header_image();
if ( $header_image ) {
$header_image_css = "
.blog__header {
background-image: url({$header_image});
}";
wp_add_inline_style( \'theme-styles\', $header_image_css );
}
现在,如果设置了,我想用特征图像替换此标题图像:
$header_image = get_header_image();
if ( has_post_thumbnail() ) {
$post_thumbnail = get_the_post_thumbnail(\'full\');
}
if ( $header_image ) {
$header_image_css = "
.blog__header {
background-image: url({$header_image});
}";
wp_add_inline_style( \'theme-styles\', $header_image_css );
} elseif ( has_post_thumbnail() ) {
$post_thumbnail_css = "
.blog__header {
background-image: url({$post_thumbnail});
}";
wp_add_inline_style( \'theme-styles\', $post_thumbnail_css );
}
然而,这不起作用,可能是因为我还没有设置帖子ID。任何帮助都将不胜感激!
最合适的回答,由SO网友:Junaid 整理而成
您没有在代码中提到或显示将上述代码挂接到哪个钩子,但我认为应该是这样的。未测试
global $post;
$header_image = \'\';
// featured image is first priority, right?
if ( has_post_thumbnail( $post->ID ) ) {
$header_image = get_the_post_thumbnail_url( $post->ID, \'full\' );
} elseif ( !empty( get_header_image() ) ) {
$header_image = get_header_image();
}
$header_image_css = ".blog__header { background-image: url({$header_image}); }";
wp_add_inline_style( \'theme-styles\', $header_image_css );