Site icon 时鹏亮的Blog

WordPress 如何获取全部有特色图片featured image的post文章

请知悉:本文最近一次更新为 5年 前,文中内容可能已经过时。

有的时候需要遍历WordPress中所有设置过特色图片文章,配合WordPress自带的方法,是可以简单实现的。

以下代码即实现了遍历WordPress中所有设置过特色图片文章并获取特色图片URL的功能:

<?php
include('wp-load.php');
/*
取有featured picture的post的查询参数
meta_query参数参考自:https://wordpress.stackexchange.com/questions/89202/query-posts-only-with-featured-image#answer-89204
*/
$args = array(
'post_type' => 'post',
'numberposts' => -1,
'posts_per_page' => -1,
'meta_query' => array(
array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS' ),
)
);
$posts = get_posts($args);
foreach ($posts as $k=>$post) {
/* 取特色图片url
参考:https://stackoverflow.com/questions/11261883/how-to-get-wordpress-post-featured-image-url
*/
$url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
$url = $url[0];
echo $post->ID."|".$post->post_title."|".$url."n";
}

相关参考:
https://wordpress.stackexchange.com/questions/89202/query-posts-only-with-featured-image#answer-89204
https://stackoverflow.com/questions/11261883/how-to-get-wordpress-post-featured-image-url


如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。

尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。


与《WordPress 如何获取全部有特色图片featured image的post文章》相关的博文:

Exit mobile version