将以下内容添加到主题的函数中。php文件将允许您使用短代码([wpse73055-random-images]
) 带有可选num
参数(即。[wpse73055-random-images num=5]
显示5个随机图像)。
function wpse73055_get_random_images( $atts ) {
extract( shortcode_atts( array(
\'num\' => 10
), $atts ) );
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' =>\'image\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => $num,
\'orderby\' => \'rand\'
);
$images_query = new WP_Query( $args );
$images = $images_query->posts;
$output = \'<ul>\';
foreach ( $images as $image ) {
$output .= \'<li>\' . wp_get_attachment_link( $image->ID ) . \'</li>\';
}
$output .= \'</ul>\';
return $output;
}
add_shortcode( \'wpse73055-random-images\', \'wpse73055_get_random_images\' );