从当前前端获取 WordPress 侧边栏 [英] Get WordPress Sidebars Fom The Current Front End

查看:22
本文介绍了从当前前端获取 WordPress 侧边栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有检索当前前端显示的 WordPress 侧边栏的功能?

Is there a function to retrieve the WordPress sidebar(s) that is displayed in the current front end?

推荐答案

我在这里发布了一个可能有效的解决方案:检查 WordPress 小部件是否显示在当前前端

I posted a solution that might work, here: Checking If A WordPress Widget Displayed In The Current Front End

dynamic_sidebar 没有注册它被调用用于那个和那个侧边栏.也没有合适的钩子来自己做这件事.因此,恐怕您必须告诉 Wordpress 为每个模板显示哪些侧边栏. 一种方法是创建这样的包装函数

dynamic_sidebar doesn't register that it's been called for that and that sidebar. Neither is there a suitable hook to do this yourself. So I'm afraid you would have to tell Wordpress for each template what sidebars are being displayed. One way to do this would be to create a wrapper function like this

function wrap_dynamic_sidebar( $sidebar_id )
{
    global $sidebars_in_this_template;
    $sidebars_in_this_template[] = $sidebar_id;
    return dynamic_sidebar( $sidebar_id );
}

并在任何地方用这个替换 dynamic_sidebar(但我知道这个解决方案很可能对您不可行).

And replace dynamic_sidebar with this everywhere (but I understand that it is very likely this solution will not be feasible for you).

如果要显示所有侧边栏的列表,可以使用 $wp_registered_sidebars

If you want to display a list of all sidebars, you could use $wp_registered_sidebars

global $wp_registered_sidebars;
$sidebar_ids = array_keys( $wp_registered_sidebars );

这篇关于从当前前端获取 WordPress 侧边栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆