我对wordpress主题开发(以及PHP)有点陌生,我很好奇是否有办法查询XX个最近的图片,可能跨越多个帖子。基本上,我想创建一个小部件,显示帖子中使用的3x3组最新图像。
你有没有想过如何做到这一点?
我对wordpress主题开发(以及PHP)有点陌生,我很好奇是否有办法查询XX个最近的图片,可能跨越多个帖子。基本上,我想创建一个小部件,显示帖子中使用的3x3组最新图像。
你有没有想过如何做到这一点?
您可以使用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.是否有任何方法可以根据帖子所选的特色图像为每篇帖子自动生成150x150的缩略图?这比手动进入每篇文章、复制150x150图像URL并将其粘贴到自定义字段中要好。