由于我的最后一个问题很快就得到了回答,我想我应该再试一次运气。
我正在尝试在我创建的自定义帖子类型中创建一个库。我希望能够通过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>';
另一种是CCD_ 2
$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()
来获得全尺寸的图像,而不是缩略图。
现在,有两个问题:
我对数组有点困惑,那么我该怎么做呢选择数组中的第一个图像,并用类"image large",然后将第二个图像包装在div是否具有"image media"类?
如何替换通过编辑器添加的图库/图像使用新的图库/图片?现在,我有两个图像,通过编辑器添加的原始图像,以及图像通过函数获得。
编辑
我想我已经解决了第一个问题。阅读关联数组,并意识到您可以执行类似echo $gallery['src'][0];
的操作来获取每个图像的源url。不过,仍然对问题2感到困惑。
想明白了。
//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' );
这删除了页面上的所有画廊。但由于我的新画廊从技术上讲并不是一个后画廊,所以它被单独留下了。