我知道如何添加图像和图库。每次加载页面时,我需要在页面上随机显示一张厨房展示的图像。
页面一次只能显示一个图像。
是否有插件或短代码来执行此操作?我知道如何使画廊的随机,但他们显示所有的图像。
Answer:
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => 1,
\'orderby\' => \'rand\',
\'post_status\' => null,
\'post_parent\' => get_the_ID(),
\'post_mime_type\' => \'image\'
);
have_posts(); //must be in the loop
the_post(); //set the ID
$images = get_children( $args );
if ($images) {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, \'full\' );
}
}
wp_reset_query();