【问题标题】:Loading wordpress plugin on specific page only仅在特定页面上加载 wordpress 插件
【发布时间】:2015-07-12 14:52:49
【问题描述】:

我正在开发一个用于在页面中加载 javascript 的 wordpress 插件。 但我希望这个插件只在选定的页面上加载。不是所有页面。 有人可以建议怎么做吗? 这是代码。

add_action('wp_enqueue_scripts', 'soundninja_enqueue');
function soundninja_enqueue($hook)
{
    wp_enqueue_script('soundninja', // id
        'http://soundninja.github.io/SNtest/build/Soundninja.min.js', // path
        array('jquery'), // dependencies
        0, // appends ?ver=$wordpress_version
        true // in_footer
    );
}

【问题讨论】:

  • 在特定页面上,意思是具有特定帖子ID的页面?
  • 两者都有!一个特定的页面和一个特定的帖子 ID。

标签: php wordpress


【解决方案1】:

另一种可能的解决方法是保持插件停用并仅在所需页面激活它。这将防止在不需要插件的页面上加载插件所涉及的额外开销。最重要的是,您不必调整现有插件的代码。

这是可以给你更多想法的帖子http://shibashake.com/wordpress-theme/how-to-selectively-load-plugins-for-specific-pages

【讨论】:

    【解决方案2】:

    根据您将调用函数soundninja_enqueue($hook) 的位置,您可以轻松添加if 语句,询问当前页面/帖子ID 是否在允许的ID 列表中。

    但首先您需要获取当前页面/帖子 ID,为此您有几个选项,具体取决于是在 The loop 内部还是外部调用函数。

    见这里how to get the current page id in wordpress

    <?php
    $allowedIds = array(12,13,14);
    if (in_array($currentID,$allowedIds)) soundninja_enqueue($hook);
    

    另一种选择是将当前页面/帖子 ID 作为参数传递给函数,并在函数内部执行相同的 if 测试,您可以选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-01
      • 2011-07-16
      • 1970-01-01
      • 2011-10-10
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      相关资源
      最近更新 更多