我们可以在get_media_embedded_in_content()
功能:
/**
* Display only the first media item in the content
*
* @link http://wordpress.stackexchange.com/a/199398/26350
*/
! is_admin() && add_filter( \'the_content\', function( $content )
{
// Get the avialable media items from the content
add_filter( \'media_embedded_in_content_allowed_types\', \'wpse_media_types\' );
$media = get_media_embedded_in_content( $content );
remove_filter( \'media_embedded_in_content_allowed_types\', \'wpse_media_types\' );
// Only use the first media item if available
if( $media )
$content = array_shift( $media );
return $content;
} , 99 );
在那里我们可以定义我们的自定义
media types
使用:
function wpse_media_types( $types )
{
return [ \'audio\', \'video\', \'object\', \'embed\', \'iframe\', \'img\' ];
}
下面是另一种方法,我们从内容中提取所有URL,并检查它是否是可用的oEmbed:
/**
* Display only the first oEmbed in the content
*
* @link http://wordpress.stackexchange.com/a/199398/26350
*/
! is_admin() && add_filter( \'the_content\', function( $content )
{
require_once( ABSPATH . WPINC . \'/class-oembed.php\' );
$wp_oembed = _wp_oembed_get_object();
$urls = wp_extract_urls( $content );
foreach( (array) $urls as $url )
{
if( $wp_oembed->get_provider( $url ) )
$content = $url;
}
return $content;
}, 1 );
请注意,您可能希望对使用此选项的位置添加进一步的限制