gpt4 book ai didi

php - 如何在 WordPress 中显示来自 get_children 数组的特定图像

转载 作者:行者123 更新时间:2023-11-28 09:09:31 25 4
gpt4 key购买 nike

如果网站某处回答了这个问题,我深表歉意,但我从昨天开始一直在搜索,但似乎找不到答案。

我正在尝试在自定义帖子类型中创建不同大小图像的网格。我需要将图像添加到编辑器中,而不是其他任何地方。自定义帖子类型在顶部有一张我使用 the_post_thumbnail() 创建的特色图片;下面是一些文字,下面我添加了一些图片。文本和图像已通过管理面板中的编辑器添加。

我不确定这是否是正确的方法,但我一直在使用 get_children() 函数来获取我上传到帖子的所有图像。这是该部分的代码:

<?php
$featuredImage = get_post_thumbnail_id( $post->ID );
$attachments = get_children( array( 'post_parent'=>$post->ID, 'post_type'=> 'attachment', 'exclude'=>$featuredImage ) );

if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );

echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="project-image w3">';
}
}
?>

这会显示除特色图片之外的所有图片。例如,我怎样才能只显示第二张图片?

最后,我想做一个 if 语句来检查添加的图像数量,并根据帖子中的图像数量,将不同的 css 类应用于 get_children 数组中的不同图像,并且显示它们,替换我通过编辑器添加的相同图像。

非常感谢您的帮助。如果我不够清楚,或者我的处理方式有误,请告诉我。

谢谢!

更新

我正在尝试用我使用 get_children() 函数创建的所需 div 中的新图像替换我通过 wordpress 编辑器添加的图像。 preg_replace() 是要走的路吗?

最佳答案

您要查找的代码片段在手册页上:

http://codex.wordpress.org/Function_Reference/get_children#Show_the_first_image_associated_with_the_post_and_re-key_the_array

这样做是将所有帖子 ID 放入一个名为 rekeyed_array 的数组中

然后您可以通过将索引 (0) 更改为您要查找的内容来引用数组来访问第一个、第二个等。

$child_image = $rekeyed_array[0];  

一旦您拥有了这种级别的控制权,那么根据附件的数量添加 css 类应该会相当简单。如果您想了解更多详情,请询问。

关于php - 如何在 WordPress 中显示来自 get_children 数组的特定图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26555010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com