仅在自定义帖子类型类别中启用自动播放

时间:2021-02-23 02:16:54

标签: woocommerce

我有一个自动自动播放音频的代码。问题在于它会在所有 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;
}

0 个答案:

没有答案
相关问题