PHP设置Wordpress帖子特色图像作为背景

时间:2016-12-02 11:22:29

标签: php wordpress

我需要将所有网页和帖子上的精选图片显示为div的背景.banner_image。我发现一些PHP在线设法让它在页面上工作,但在帖子上没有任何反应。任何人都可以告诉我如何修改以下代码以从WP Posts中提取图像?

function cs_single_bg() {
if(is_page())
{
    $cs_page_id = get_queried_object_id();
    $cs_post_thumbnail_id = get_post_thumbnail_id( $cs_page_id );
    $cs_bkgdImg = wp_get_attachment_url( $cs_post_thumbnail_id );        
    ?>
        <style>
            .banner_image { 
                background-image: url(<? echo $cs_bkgdImg ?>);
                 background-repeat: no-repeat;
                background-position: center center;
                }
        </style>
     <?    }
}

1 个答案:

答案 0 :(得分:0)

在WP_DEBUG标志开启后始终进行开发,

function cs_single_bg() {
if(is_singular()) // Check a page in case post is_post()
{
    $cs_page_id = get_queried_object_id();
    $cs_post_thumbnail_id = get_post_thumbnail_id( $cs_page_id );
    $cs_bkgdImg = wp_get_attachment_url( $cs_post_thumbnail_id );        
    ?>
        <style>
            .banner_image { 
                background-image: url(<?php echo $cs_bkgdImg ?>);
                 background-repeat: no-repeat;
                background-position: center center;
                }
        </style>
     <?    }
}

你错过了

        background-image: url(<?php echo $cs_bkgdImg ?>);