php - WP 动态特色图片 - 无法获取第二个特色图片网址

标签 php wordpress image wordpress-featured-image

我实际上正在开发一个带有 Dessign.net 主题(像素主题)的 wordpress 网站,该网站在首页上有一个漂亮的整页 slider 。 slider 显示所选帖子的特色图片(我已在编辑页面的元框字段中选中“在幻灯片中显示”的帖子)。

这些特色图片以相同的方式用于网站上的不同 View (例如缩略图)。我需要它们作为缩略图,但我想要主页 slider 的另一张图片(仍然相对于选定的帖子)。

我发现了 wordpress 的“动态特色图片”插件,但现在我无法在 slider 的循环中获取第二个特色图片 url。

这是 slider 的代码部分,就像主题一样:

<ul>
        <?php
        $slider_arr = array();
        $x = 0;
        $args = array(
             //'category_name' => 'blog',
             'post_type' => 'post',
             'meta_key' => 'ex_show_in_slideshow',
             'meta_value' => 'Yes',
             'posts_per_page' => 99
             );
        query_posts($args);
        while (have_posts()) : the_post();



             $thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'full' );
            //$thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'large' );
            $img_url = $thumb['0'];
        ?>
            <li data-background="<?php echo $img_url; ?>" onclick="location.href='<?php the_permalink(); ?>';" style="cursor:pointer;">

            </li>
        <?php array_push($slider_arr,get_the_ID()); ?>
        <?php $x++; ?>
        <?php endwhile; ?>
        <?php wp_reset_query(); ?>

    </ul>

现在我尝试将在插件 github 页面上找到的代码放在:

if( class_exists('Dynamic_Featured_Image') ) {
               global $dynamic_featured_image;
              $thumb = $dynamic_featured_image->get_featured_images( );

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

              }

代替 $thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'full' );

但是 $thumb 返回 array 作为字符串

我尝试了一些不同的东西,但我对 php 不是很流利。

希望这是可以理解的。

最佳答案

我最近不得不自己寻找类似问题的答案。插件作者很擅长解释如何实际设置插件,但并没有真正说明如何获取图像,将其留给开发人员。所以,我感觉到你。

如果我没理解错的话,您需要从插件中获取特色图片,而不是 WordPress 中包含的特色图片。

<?php global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images( get_the_ID() );

//You can now loop through the image to display them as required
foreach($featured_images as $featured_image) {

echo "<a href='".get_the_permalink()."' class='slide'>";
echo "<span> <img src='".$featured_image['full']."' /> </span>";
echo "</a>";

} ?>

在此插件中,您可以为每个帖子/页面创建无限数量的特色图片。上面的代码仅用于抓取插件创建的第一张图片。调用图像本身的是 $featured_image['full']

您也可以将显示的图像类型更改为其他尺寸,包括您创建的任何自定义尺寸。使用这些尺寸的代码可以在 this post. 上找到

关于php - WP 动态特色图片 - 无法获取第二个特色图片网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30079269/

相关文章:

php - 添加源以构建 php 扩展

php - 在 WordPress 中向编辑器显示仅限管理员的子菜单链接。结果出错

WordPress/类别/子类别 404 错误

python - 如何在图像比例变大时围绕其中心旋转图像(在 Pygame 中)

css - 左对齐文本中的居中图像

php - php 站点是否仅使用 mysql SELECT 免受 sql 注入(inject)攻击

php - OpenEMR安装错误步骤3

php shell_exec() 命令弹出错误和权限被拒绝 ffmpeg

javascript - W3 Total Cache 链接到 http 而不是 https

image - 将带有图像的 Canvas 导出为图像(如 PNG 或 jpg)