有的时候需要遍历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
如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。
如本文对您有用,捐赠和留言 将是对我最好的支持~(捐赠可转为站内积分)
如愿意,请向朋友推荐本站,谢谢。
尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。