【问题标题】:Replacing a Wordpress Image gallery替换 Wordpress 图片库
【发布时间】:2014-10-27 16:32:45
【问题描述】:

由于我的上一个问题很快得到了回答,我想我会再试试运气。

我正在尝试在我创建的自定义帖子类型中创建画廊。我希望能够通过 wordpress 管理编辑器将图像/图库添加到帖子中,然后有一个功能拉图像,将它们包装在 div 中,并用新图像替换现有的图库。

我想这样做是因为我希望图像适合不同大小的图像网格。例如,图像 1 将是全宽,图像 2 将是一半宽度,图像 3 将是四分之一等。

我试过两种方法,一种是get_children()

$featuredImage = get_post_thumbnail_id( $post->ID );
$imageArgs = array(
    'numberposts' => 5,
    'order' => 'DESC',
    'post_mime_type' => 'image',
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'exclude' => $featuredImage
    );
$attachments = get_children($imageArgs, ARRAY_A);
$rekeyed_array = array_values($attachments);
$child_image = $rekeyed_array[0];
echo '<div class="project-img"><img src="' . $child_image['guid'] . '" class="project-image"></div>';
$child_image = $rekeyed_array[1];
echo '<div class="project-img w2"><img src="' . $child_image['guid'] . '"></div>';
$child_image = $rekeyed_array[2];
echo '<div class="project-img w3"><img src="' . $child_image['guid'] . '"></div>';
echo '<div class="project-img w3"><img src="' . $child_image['guid'] . '"></div>';

另一个是get_post_gallery()

$gallery = get_post_gallery( get_the_ID(), false );

            /* Loop through all the image and output them one by one */
            foreach( $gallery['src'] AS $src )
            {
                ?>
                <div class="project-img">
                <img src="<?php echo $src; ?>" alt="Gallery image" />
                </div>
                <?php
            }

我在使用get_post_gallery() 方面没有取得太大进展,但我有点明白我将不得不使用wp_get_attachment_url() 来获取完整大小的图像而不是缩略图。

现在,有两个问题:

  1. 我对数组有点困惑,那我该怎么做 选择数组中的第一个图像并将其包装在 div 中 类“image-large”,然后是第二个图像并将其包装在一个 具有“图像介质”类的 div?

  2. 如何替换通过编辑器添加的图库/图像 有了新的画廊/图片?现在,我得到了两个实例 图像,通过编辑器添加的原始图像,以及图像 通过函数获得。

编辑

我想,我想出了问题 1。阅读关联数组并意识到您可以执行echo $gallery['src'][0]; 之类的操作来获取每个图像的源网址。不过,仍然对问题 2 感到困惑。

【问题讨论】:

    标签: php wordpress image image-gallery


    【解决方案1】:

    想通了。

    //Remove original Gallery
    function remove_the_first_gallery( $output, $attr ){
        $output = '<!-- gallery 1 was here -->';   // Must be non-empty.
        return $output;
    }
    
    add_filter( 'post_gallery', 'remove_the_first_gallery' );
    

    这删除了页面上的所有画廊。但由于我的新画廊在技术上不是 post_gallery,所以它被单独留下了。

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      相关资源
      最近更新 更多