【问题标题】:If wordpress widget is not active, show message如果 wordpress 小部件未激活,则显示消息
【发布时间】:2012-08-29 15:24:25
【问题描述】:

我正在编写插件,我需要检查一个小部件是否在使用中。如果小部件正在使用,那很好,但如果不是,我想显示错误消息。

我尝试的代码是这样的,但它不起作用。我没有收到任何消息。

function my_widgie_detect()  {
  if ( false === is_active_widget( 'testimonial' )) {
    // do something here if the widget isn't active;
    echo '<div id="message" class="updated fade"><p>No active</p></div>';
  } else {
    echo '<div id="message" class="updated fade"><p>Active</p></div>';
   }
}
add_action( 'wp_head', 'my_widgie_detect' );

那么如何识别widget是否开启,如果没有的话wordpress会显示消息呢?

【问题讨论】:

    标签: plugins widget wordpress


    【解决方案1】:

    我的一个客户想检查是否有从模板文件激活到侧边栏中的搜索小部件,所以这里是代码,我猜这就是你需要的!我希望它也能帮助其他人!

    <?php if(is_active_widget(false,false,'search')){
    echo "Search Widget is activated"; //We don't need to place the search box here, since we have already into the sidebar.
    }else{
    get_search_form();
    }
    ?>
    

    【讨论】:

    • 不要使用 init 或 wp_head 钩子来输出结果,就像你和其他用户在他们的代码上所做的那样,使用模板钩子来输出你的结果。
    【解决方案2】:

    让它像这样工作:

    function check_widget() {
        if( is_active_widget( '', '', 'testimonial' ) ) { // check if search widget is used
            //yay! active
        } else { echo '<div id="message" class="updated fade"><p><strong>Widget is not active! Remembet to activate it here!</p></div>'; }
    }
    
    add_action( 'init', 'check_widget' );
    

    【讨论】:

      【解决方案3】:

      您正在使用wp_head 操作,因此您的消息 div 将位于头部并且不会显示...查看http://codex.wordpress.org/Plugin_API/Action_Reference 以使用其他操作,或者只是修改您的模板。

      【讨论】:

        猜你喜欢
        • 2016-11-09
        • 1970-01-01
        • 2017-01-17
        • 2015-09-06
        • 1970-01-01
        • 2012-05-31
        • 2013-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多