【问题标题】:Dynamic Featured Image动态特色图片
【发布时间】:2013-11-30 00:24:39
【问题描述】:

我最近为 wordpress 安装了动态特色图片插件。但我不知道如何链接图像。我正在尝试为我创建一个这样的画廊http://www.subcreative.com.au/#work - 向下滚动到项目,你会看到。

我已经把这段代码放在functions.php中

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

   if( function_exists('dfi_get_featured_images') ) {
       $featuredImages = dfi_get_featured_images();

       //Now, loop through the image to display
   }

   endwhile;
?>

并用它来链接图像。

echo ' <a class="fancybox" href="'. dfi_get_featured_images() .'" style="text-align:center">Take a look</a> '; ?>

但是当我尝试打开图片时,它变成了“/array”

【问题讨论】:

  • 错误告诉你去哪里看。您需要从已创建的数组中选择一个项目。您可能需要上传比这更多的代码来帮助我们,看起来有其他 PHP 正在与此对话,例如 dfi_get_features_images() 函数的代码

标签: php arrays wordpress dynamic-featured-image


【解决方案1】:

我不是 wordpress 开发人员,但我在我试图修复的 wordpress 网站上看到了这个。 所以也许你可以试试这个。

if( class_exists('Dynamic_Featured_Image') ):
    global $dynamic_featured_image;
    global $post;
     $featured_images = $dynamic_featured_image->get_featured_images( $post->ID );

     if ( $featured_images ):
        ?>
            <?php foreach( $featured_images as $images ): ?>
               <img src="<?php echo $images['full'] ?>" alt="">
            <?php endforeach; ?>
        <?php
        endif;
endif;

这适用于我的情况。我正在使用 DFI 3.1.13

【讨论】:

  • 完美运行谢谢,我们有机会从这个 DFI 中获得 alt 属性,谢谢...罗德里戈
【解决方案2】:

此答案仅对插件版本 2.0.2 及以下版本有效。

您需要遍历返回的数组并手动显示图像。试试这个:

<?php   

    if( function_exists('dfi_get_featured_images') ) {
       $featuredImages = dfi_get_featured_images();

       //Loop through the image to display your image

       if( !is_null($featuredImages) ){

            $links = array();

            foreach($featuredImages as $images){
                $thumb = $images['thumb'];
                $fullImage = $images['full'];

                $links[] = "<a href='{$fullImage}' class='dfiImageLink'><img src='{$thumb}' /></a>";
            }

            echo "<div class='dfiImages'>";
            foreach($links as $link){
              echo $link;
            }                
            echo "</div>";
         }        
    }

?>

【讨论】:

  • 我应该把这个放在哪里?函数.php ?我的 CPT 吗?
  • 你需要把它放在你想显示图片的地方。例如,如果您想在主索引页面中显示图像,您可以在 index.php`` just after the statement `.
  • 哈哈,当我将代码放入我的模板时,它会创建十亿个帖子。 @控制台
  • 我打赌你在循环中使用循环。您只需要一个 while 语句。删除一个 while 循环。
  • 如我所说。您正在循环内使用循环。再次检查答案。我已经从我的答案中删除了 while 循环。你已经在一个循环中,所以你不需要它。
【解决方案3】:

在有帖子循环中试试这个

$img=dfi_get_featured_images();
$url=$img['full'];
echo ' <a class="fancybox" href="'. $full .'" style="text-align:center">Take a look</a> ';

如果 full 不起作用,也试试 thumb。

【讨论】:

    猜你喜欢
    • 2015-07-16
    • 2012-01-16
    • 2012-09-03
    • 2018-04-06
    • 2019-07-31
    • 2015-02-15
    • 2015-04-28
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多