Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active June 1, 2020 09:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glueckpress/2430421 to your computer and use it in GitHub Desktop.
Save glueckpress/2430421 to your computer and use it in GitHub Desktop.
[WordPress] Check if the current widget is on first position in its sidebar
<?php
/**
* Goes in function widget(…) inside class Your_Widget
*/
// 1. Get all active widgets from all sidebars.
$all_sidebars_widgets = wp_get_sidebars_widgets();
// 2. Get only the active widgets in our currently inhabitated sidebar.
$this_sidebars_widgets = $all_sidebars_widgets[ $args[ 'id' ] ]; // Current sidebar ID
// 3. Check if this widget is the first one in its sidebar.
$is_first_widget = ( $this_sidebars_widgets[ 0 ] == $this->id ) ? true : false;
// var_dump( $is_first_widget );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment