这里有一种方法,在许多方法中,我们使用the_content
过滤并循环通过get_attached_media( $type )
构建连接媒体的列表。例如,我们可以使用$type = \'image\'
对于图像或$type = null
对于所有mime类型。
add_filter( \'the_content\', function( $content )
{
// Nothing to do - target single posts assigned to the \'report\' category
if( ! in_the_loop() || ! is_single() || ! has_category( \'report\' ) )
return $content;
// Reduce attached media items into a string
$li = array_reduce(
get_attached_media( $type = null ), // Fetch the attached media (all == null)
function( $li, $item ) // Callback for each media item
{
$li .= sprintf( // Construct and append each list item
\'<li>%s</li>\',
wp_get_attachment_link( $item->ID, false )
);
return $li;
},
\'\' // Initial value of $li
);
// Append to the content
if( $li )
$content .= sprintf( \'<ul class="attached-media">%s</ul>\', $li);
return $content;
}, 999 ); // Some late priority
此处,我们仅显示分配给
report
类别