0

我已经安装了最新的 Dynamic Featured Image 3.1.2,当我尝试在页面上打印出精选图像数组时,我只能得到 Featured Image 2 及以后的版本。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php if( class_exists('Dynamic_Featured_Image') ) {
        global $dynamic_featured_image;
        $featured_images = $dynamic_featured_image->get_featured_images( );

        print_r( $featured_images );

        //You can now loop through the image to display them as required
    } ?>

    ... << rest of Post Loop >>

尽管我添加了不止一张特色图片,但它仅在特色图片 2 及阵列中显示。

我错过了我需要做的其他事情吗?

4

2 回答 2

0

您在代码中打印的是由插件 Dynamic Featured Image 3.1.2 添加的自定义特色图像(特色图像 2 以后):

<?php if( class_exists('Dynamic_Featured_Image') ) {...

..而您的编辑帖子屏幕中的第一个“特色图像”是在 wordpress 核心本身中实现的,并且可以使用该the_post_thumbnail功能进行打印(请参阅http://codex.wordpress.org/Function_Reference/the_post_thumbnail)。

于 2014-07-30T23:15:30.137 回答
0

您可以使用此功能获取所有特色图像,包括默认的 WordPress 图像:

/**
 * Retrieve featured images for specific post(s) 
 * including the default Featured Image
 *
 * @since 3.1.2
 * @access public
 *
 * @see  $this->get_featured_images()
 *
 * @param Integer $post_id id of the current post
 *
 * @return Array An array of images or an empty array on failure
 */
public function get_all_featured_images( $post_id = null ) {

    if ( is_null( $post_id ) ) {
        global $post;
        $post_id = $post->ID;
    }

    $thumbnail_id = get_post_thumbnail_id( $post_id );

    $featured_image_array = array();
    if ( ! empty( $thumbnail_id ) ) {
        $featured_image = array(
            'thumb' => wp_get_attachment_thumb_url( $thumbnail_id ),
            'full' => wp_get_attachment_url( $thumbnail_id ),
            'attachment_id' => $thumbnail_id
        );
        $featured_image_array[] = $featured_image;
    }

    $dfiImages = $this->get_featured_images( $post_id );

    $all_featured_images = array_merge( $featured_image_array, $dfiImages );

    return $all_featured_images;

}

此功能将包含在下一个版本的插件中。在此之前,您可以将其添加到您的dynamic-featured-image.php插件文件中,或者您可以从github的master 分支下载并使用该文件。

PS我是插件的作者。

于 2014-08-01T02:13:47.190 回答