问题描述
我创建了一个名为画廊"的页面,并创建了带有 10 张图像的 新画廊
.我的页面 id 是 129.我在那个 page id(129)
中有 10 张图片.现在我的问题是,我需要一个代码来在 wordpress 中获取这 10 张图片.请任何人帮我一个代码.提前致谢.
I have created a page called "Gallery" and created new gallery
with 10 images. My page id is 129. I have 10 images in that page id(129)
.Now my question is, i need a code to fetch those 10 images in wordpress. please anyone help me with a code. Thanks in advance.
推荐答案
使用 get_children
我使用此代码按所选顺序从页面库中提取所有图像.您可以将此代码包含在循环中或单独使用.只需选择适当的 post_parent 代码(参见下面的代码示例).
I used this code to extract all the images from a page gallery in the chosen order. you can include this code in the loop or use it stand alone. just choose the appropriate post_parent code (see bellow the code example).
此示例显示与页面 ID 129 相关联的所有图像,请看:
This example show all images associated to the page id 129, have a look:
$images = get_children( array( 'post_parent' => 129, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
$images 现在是一个对象,它包含所有图像(与帖子 ID 1 相关)及其信息的顺序,就像画廊界面一样.
$images is now a object that contains all images (related to post id 1) and their information ordered like the gallery interface.
if ( $images ) {
//looping through the images
foreach ( $images as $attachment_id => $attachment ) {
?>
<?php /* Outputs the image like this: <img src="" alt="" title="" width="" height="" /> */ ?>
<?php echo wp_get_attachment_image( $attachment_id, 'full' ); ?>
This is the Caption:<br/>
<?php echo $attachment->post_excerpt; ?>
This is the Description:<br/>
<?php echo $attachment->post_content; ?>
<?php
}
}
找到您要从中提取图像的帖子 ID 并将其插入到此参数中:'post_parent' => 129
Find the post id you wan to extract images from and insert it into this argument: 'post_parent' => 129
你也可以使用:
'post_parent' => $post->ID如果你想在一个循环中使用 get_children,并从返回的 post id 中获取 post id.
'post_parent' => $post->IDIf you want to use get_children in a loop, and get the post id from the returned post id.
如果您想排除被选为特色图片的图片,我将使用 if 语句检查图片网址是否与特色图片网址相同.
If you want to exclude the image selected as a featured image i would have a if statement check if the image URL is equal to the featured image URL.
这篇关于如何从wordpress中的特定页面ID获取所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!