要计算一张图片所附的帖子数量并不容易,WordPress根本无法跟踪。它只是跟踪附件最初上传到的帖子(甚至不一定在那里使用)。
插件
为了让您尽快开始,以下是插件代码:<?php
/**
* Plugin Name: Media Count
* Description: Adds a column to the media admin list table to show the count of posts
*/
add_filter( \'manage_media_columns\', function( $cols, $detached )
{
$cols[\'count\'] = \'Count\';
$cols[\'size\'] = \'Size\';
return $cols;
}, 10, 2 );
add_action( \'manage_media_custom_column\', function( $col, $id )
{
switch ( $col )
{
case \'size\' :
$meta = wp_get_attachment_metadata( $id );
// Image
isset( $meta[\'width\'] )
AND print "{$meta[\'width\']} × {$meta[\'height\']}";
// Audio
isset( $meta[\'bitrate\'] )
AND print "{$meta[\'length_formatted\']} min";
break;
case \'count\' :
$att = get_post_custom( $id );
$file = $att[\'_wp_attached_file\'][0];
// Do not take full path as different image sizes could
// have different month, year folders due to theme and image size changes
$file = pathinfo( $file, PATHINFO_FILENAME );
// @TODO Fill in the blanks
break;
}
}, 10, 2 );
问题:如何计算附件使用的帖子数量-最有效的方法。