wordpress 计数小部件 [英] wordpress count widgets

查看:31
本文介绍了wordpress 计数小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚我们如何获得在给定侧边栏位置发布的小部件的数量.

Hi I am trying to figure out as to how can we get the count of widgets published in a given sidebar position.

例如,我有一个名为 UTILITY 的侧边栏,它是一个水平侧边栏.我希望这些小部件位置的宽度根据其中发布的小部件数量进行平均调整.

For example I have a sidebar called UTILITY which is a horizontal sidebar. I want the width of these widget positions to adjust equally according to the number of widgets published in it.

我想知道在给定页面上有多少小部件发布在此侧边栏中,以便我可以根据它分配宽度类.

I want to know that on a given page how many widgets are published in this sidebar so that I can assign a width class according to it.

推荐答案

wp_get_sidebars_widgets()

会给你一个侧边栏和它们拥有的小部件的数组,只需计算适当侧边栏的数组..

Will give you an array of the sidebars and the widgets they have, just count the array for the appropriate sidebar..

例如:

$the_sidebars = wp_get_sidebars_widgets();
echo count( $the_sidebars['my-sidebar-id'] );

ID 是您注册侧边栏时声明的 ID(因此请检查您的侧边栏注册码).

The ID is the one you declare when you register the sidebar(so check your sidebar registration code).

你也可以把它包装成一个函数.

You could also wrap this into a function.

function count_sidebar_widgets( $sidebar_id, $echo = true ) {
    $the_sidebars = wp_get_sidebars_widgets();
    if( !isset( $the_sidebars[$sidebar_id] ) )
        return __( 'Invalid sidebar ID' );
    if( $echo )
        echo count( $the_sidebars[$sidebar_id] );
    else
        return count( $the_sidebars[$sidebar_id] );
}

然后在需要获取计数时调用它,使用侧边栏 ID..

Then call it when you need to get the count, using the sidebar ID..

count_sidebar_widgets( 'some-sidebar-id' );

或存储在变量中以供其他用途..

Or store in variable for other usage..

$my_var = count_sidebar_widgets( 'some-sidebar-id', false );

希望有帮助..

这篇关于wordpress 计数小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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