从WordPress中的自定义元设置背景图像


Set Background Image from Custom Meta in WordPress

我正在尝试获取我正在构建的WordPress主题的主页,以在刷新时基于3个潜在的背景图像来更改背景图像。

每个图片都被认为是一个更大的"案例研究"的一部分,该研究有标题、链接、文本等。案例研究字段是通过主页上的自定义元框创建的。(我使用它来简化元框的创建过程:https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress)。

长话短说,客户希望主页上的3个案例研究都有相应的背景图像。这就是为什么我没有使用特色图像功能,因为每个背景都有特定的元数据。

问题是,我不知道如何获得所选背景图像的元ID,并使用它来设置背景CSS。

以下是我目前所拥有的:

/**
 * Home Page Case Randomizer
 */
$GLOBALS['_home_case_number'] = rand(1, 3);

function home_case_number() {
   if ( is_front_page() ) :  // checks whether this is the home page
    // get the meta image
$attachment_image = get_post_meta( get_the_ID(), '_home_case_background_' . $_home_case_number, true );

这就是我感到困惑的地方。以上内容返回相应的image.jpg文件。如何使用下面的代码对此进行按摩?

以下代码改编自http://s2webpress.com/responsive-featured-image-function-in-wordpress-themes/,我想用它来确保我提供的图像具有响应性。

// store the image sizes in an array
$img_sizes = array( 'thumbnail', 'medium', 'large', 'full' );
// grab the URL for each image size and store in a variable
foreach ( $img_sizes as $img_size ) {
    ${ 'img_src_' . $img_size } = wp_get_attachment_image_src( $attachment_image, $img_size );
}


echo '<style type="text/css"> 
    .featured-image {
        width: 100%;        
        min-height:350px; 
        background-image: url(' . esc_url( $img_src_medium[0] ) . ');
    }

    @media screen and ( min-width: 400px ) {
        .featured-image {
            background-image: url(' . esc_url( $img_src_large[0] ) . ');
        }
    }
    @media screen and ( min-width: 1000px ) {
        .featured-image {
            background-image: url(' . esc_url( $img_src_full[0] ) . ');
        }               
    }

</style>';
endif;
 };

add_action( 'wp_head', 'home_case_number' );

那么我应该通过获取背景图像的元ID来完成这项工作吗?还是附件ID?在这一点上我都不知道该怎么做。

非常感谢您的帮助!

我想通了!

1) CMB"插件"通过在其末尾添加"_ID"将附件ID保存为另一个元密钥,所以我用它来获取附件ID2) 我有可变范围的问题。当我在函数中调用全局变量时,事情就开始工作了。这是最后的代码:

/**
 * Home Page Case Randomizer
 */
$GLOBALS['_home_case_number'] = rand(1, 3);


function home_case_number() {
    global $_home_case_number;
   if ( is_front_page() ) :  // checks whether this is the home page

    // get the attachment thumbnail ID
            $attachment_image = get_post_meta( get_the_ID(), '_home_case_background_' . $_home_case_number . '_id', true );

    // store the image sizes in an array
    $img_sizes = array( 'thumbnail', 'medium', 'large', 'full' );
    // grab the URL for each image size and store in a variable
    foreach ( $img_sizes as $img_size ) {
        ${ 'img_src_' . $img_size } = wp_get_attachment_image_src( $attachment_image, $img_size );
    }

    echo '<style type="text/css"> 
        .featured-image {
            width: 100%;        
            min-height:350px; 
            background-image: url(' . esc_url( $img_src_medium[0] ) . '); 
        }

        @media screen and ( min-width: 400px ) {
            .featured-image {
                background-image: url(' . esc_url( $img_src_large[0] ) . ');
            }
        }
        @media screen and ( min-width: 1000px ) {
            .featured-image {
                background-image: url(' . esc_url( $img_src_full[0] ) . ');
            }               
        }

    </style>';
    endif;
 };

add_action( 'wp_head', 'home_case_number' );

希望这能帮助其他人。