【问题标题】:Wordpress Genesis theme display a custom header/background image using featured imageWordpress Genesis 主题使用特色图像显示自定义标题/背景图像
【发布时间】:2016-12-28 18:25:59
【问题描述】:

我正在使用 Genesis 框架构建一个站点。我在向每个页面添加自定义标题并使用管理区域中的特色图片链接发布时遇到了问题。现在,特色图片标题不会显示在我指定为“阅读”设置下的“博客”页面的页面上。

这是网站 URL,http://ajcustomfinishes.starfireclients.com/。

这是我正在使用的功能:

// Create new image size for our hero image
add_image_size( 'hero-image', 1400, 400, TRUE ); // creates a hero image size

// Hook after header area
add_action( 'genesis_after_header', 'bw_hero_image' );

function bw_hero_image() {
// If it is a page and has a featured thumbnail, but is not the front page do the following...
    if (has_post_thumbnail() && is_page() ) {
        // Get hero image and save in variable called $background
        $image_desktop = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'hero-image' );
        $image_tablet = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'large' );
        $image_mobile = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'medium' );

        $bgdesktop = $image_desktop[0];
        $bgtablet = $image_tablet[0];
        $bgmobile = $image_mobile[0];

// You can change above-post-hero to any class you want and adjust CSS styles
        $featured_class = 'above-post-hero';

 ?> 
<div class='<?php echo $featured_class; ?>'><h1 class="custom-title">AJ Customs Finishes in Las Vegas<br>Call 702-795-7338 today!</h1></div>
<style>
    <?php echo ".$featured_class "; ?> {background-image:url( <?php echo $bgmobile; ?>);height:176px;}

        @media only screen and (min-width : 480px) {       
        <?php echo ".$featured_class "; ?> {background-image:url(<?php echo $bgtablet;?>);height:276px;}
        }
        @media only screen and (min-width : 992px) {       
        <?php echo ".$featured_class "; ?> {background-image:url(<?php echo $bgdesktop;?>);height:400px;}
        }
</style>
<?php
    } 
}

有人知道我如何使用特色图片在每个页面上显示自定义标题吗?

【问题讨论】:

    标签: php css wordpress genesis


    【解决方案1】:

    您的代码中有此条件,如果您在阅读设置中设置为博客的页面,则该条件为 FALSE:

        if (has_post_thumbnail() && is_page() ) {
    

    is_page() 在博客帖子主页(您为阅读中的帖子设置的页面)上将为 false。

    您需要将此条件设置为 is_home() 为真,即您可以将代码重新调整为:

        if (has_post_thumbnail() && is_page() || is_home()) {
    

    【讨论】:

    • 非常感谢。像魅力一样工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    相关资源
    最近更新 更多