有的时候需要遍历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



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

留言