我有一个自动自动播放音频的代码。问题在于它会在所有 woocommerce 产品中自动播放。我想知道是否有办法只为特定的 woocommerce 类别启用它?
add_filter( 'woocommerce_short_description', 'wpse_67108_autplay_music' );
function wpse_67108_autplay_music( $content )
{
if ( ! is_singular( 'product' ) )
{
return $content;
}
$audio_files = get_children(
array (
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'audio'
)
);
$audio = '';
if ( $audio_files )
{
$id = array_pop( array_keys( $audio_files ) );
$url = wp_get_attachment_url( $id );
// add a 'controls' attribute to enable controls
$autoplay = in_category( 'premium' ) ? 'autoplay' : '';
$audio = "<audio src='$url' controls controlsList = 'nodownload' $autoplay loop></audio>";
}
return $audio . $content;
}