我们如何才能看到哪些帖子没有特色图片,或者它不再存在?

时间:2012-05-10 作者:Alex

我需要哪一个插件或代码来制作它?向我显示一个没有特色图片或已被破坏(特色图片不再存在)的帖子列表?

1 个回复
最合适的回答,由SO网友:brasofilo 整理而成

以短代码的形式:

add_shortcode(\'nofeatures\', \'wpse_51768_shortcode\');
function wpse_51768_shortcode() {
    $posts = get_posts( array(\'numberposts\' => -1) );
    foreach ( $posts as $post ) {
        $featured = get_the_post_thumbnail( $post->ID, \'thumbnail\', null );
        if ( $featured ) echo \'Has Featured Image: \' . $post->post_title . \'<br />\';
    }
}

[update]

以下输出了don\'t have a Featured Image, 如果有,请检查文件是否live:

add_shortcode(\'nofeatures\', \'shortcode_wpse_51768\');

function shortcode_wpse_51768() 
{
    $args = array(
        \'numberposts\' => -1,
        \'post_type\'   => \'post\',
        \'post_status\' => \'publish\'
    );
    $posts = get_posts( $args );

    foreach ( $posts as $post ) 
    {
        $featured = has_post_thumbnail( $post->ID );

        if ( !$featured ) 
            echo "<p>Doesn\'t have Featured Image: <b>" . $post->post_title . "</b></p>";
        else
        {
            $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'full\' );
            $is_live = check_if_featured_image_is_live( $thumb[0] );
            if ( !$is_live )
                echo \'<p>Featured image 404: <b>\' . $post->post_title . \'</b></p>\';
        }
    }
}

// http://stackoverflow.com/a/7953100/1287812
function check_if_featured_image_is_live($url)
{
    $options[\'http\'] = array(
        \'method\' => "HEAD",
        \'ignore_errors\' => 1,
        \'max_redirects\' => 0
    );
    $body = file_get_contents($url, NULL, stream_context_create($options));
    sscanf($http_response_header[0], \'HTTP/%*d.%*d %d\', $code);
    return $code === 200;
}

结束

相关推荐

Featured Images on Front Page

我目前有一个静态首页和一个/博客。我的目标是让头版成为手工选择的图像网格(类似于公文包)。我希望能够轻松切换此页面上的图像,而无需更改原始html。最好的方法是在页面上使用自定义字段吗?还是有更好的方法来管理静态页面上的特色图像?谢谢