index of current widget in sidebar (2 posts)

  1. kkarpieszuk
    Posted 4 years ago #


    i wonder if i had some opportunity to check using PHP index of position of widget in sidebar.

    i am going to create some widget (i think i will do this with help of this tutorial http://www.lonewolfdesigns.co.uk/create-wordpress-widgets/). this widget will show number, same as position of widget in sidebar. how to get this number?

    if it is still unclear:

    imagine that somebady has widgetized sidebar on his blog. he put there few diffrent widgets and one is mine. this widget have to show message "hello, i am widget number 4 in this sidebar" (if widget will be placed as fourth, otherwise number will be difffrent)

    any idea? thank you in advance for any suggestions :)

  2. iron77
    Posted 4 years ago #

    You can hook into 'dynamic_sidebar' action, which runs every time before widget is being viewed in front-end. It can be for example just a function with an incrementing global variable (if you don't hate them too much :)).

    Assuming there might be more than one sidebar, you'll also need to reset the counter. You can possibly use 'sidebars_widgets' action hook for that.

Topic Closed

This topic has been closed to new replies.

About this Topic