您可以使用get_posts
或创建新的WP_Query
使用以下参数(或类似的参数)。
<?php
$args = array(
\'post_type\' => \'attachment\', // attachment post type
\'post_status\' => \'inherit\', // all attachments have this post status
\'post_mime_type\' => \'image\', // make sure you get images only
\'posts_per_page\' => 5 // however many images you want
);
在循环浏览图像时,可以使用
wp_get_attachment_image
或
wp_get_attachment_image_src
分别获取图像HTML或图像URL。
<?php
$attachments = get_posts($args); // args from above
foreach($attachments as $a)
{
// replace `thumbnail` with an appropriate image size
echo wp_get_attachment_image($a->ID, \'thumbnail\');
}
您还需要阅读
widgets API 用于创建小部件。法典有一个
basic example. 还有很多教程,下面是
one I wrote.