动态特色图片 - WordPress - 没有获得第一个特色图像

问题描述 投票:0回答:2

我已经安装了最新的动态特色图像3.1.2,当我尝试在页面上打印出特色图像数组时,我只获得了特色图像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中,然后在阵列中显示。

我有没有想念我需要做的其他事情?

wordpress wordpress-plugin dynamic-featured-image
2个回答
0
投票

您在代码中打印的内容是插件动态特色图像3.1.2添加的自定义特色图像(特色图像2以后):

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

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


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 branch中的文件。

附:我是plugin的作者。

© www.soinside.com 2019 - 2024. All rights reserved.