Wordpress音频播放器和平滑状态.在新页面重新加载播放器时调用什么回调函数

Wordpress Audio Player and SmoothState. What to call on callback to reload player on new pages?

本文关键字:播放器 加载 什么 函数 回调 调用 新加载 平滑 音频 状态 新页面      更新时间:2023-09-26

我在"header.php"页面上使用一个带有短代码的Wordpress音频播放器。

我也使用SmoothState (http://miguel-perez.github.io/smoothState.js/)。

我必须调用什么函数"回调"重新加载播放器的新页面?

我在functions.php中查询js和css,像这样:

wp_enqueue_style( 'wp-mediaelement' );
wp_enqueue_script( 'wp-mediaelement' );

wp_enqueue_script();所做的就是在检查依赖关系后将脚本放入页面。它不会让你在不同的页面重新加载游戏。但是,只要您正确使用这个函数,您的脚本应该在每个页面上加载。也许你的错误在别处?

来自wp_enqueue_script();上的WordPress文档:

在适当的时间将脚本文件链接到生成的页面到脚本依赖项,如果脚本还没有包含,如果所有依赖项都已注册。

如果Wordpress Audio Player有jQuery作为依赖(可能),你的functions.php文件应该像这样:

function wpaudioscript() {
    // Parameters are name, path, dependency, version, loadinfooter
    // Better explanation here: http://codex.wordpress.org/Function_Reference/wp_enqueue_script
    wp_enqueue_script(
        'name-of-script',
        get_stylesheet_directory_uri() . '/js/custom_script.js',
        array( 'jquery' ),
        1.0.0,
        true
    );
}
add_action( 'wp_enqueue_scripts', 'wpaudioscript' );

其他注意事项:

    您应该在应用程序的页脚加载脚本以获得更好的性能。将wp_enqueue_script的第五个参数设置为true,如上面的示例所示,将为您完成此操作。许多人通常将这样的自定义功能放入他们的functions.php文件中,但我发现将所有自己的修改放入自定义模块中要干净得多。Smashing杂志是一个很好的关于如何做到这一点的概述。