1

I'm using this code to display widgets of certain sidebar:

$sidebar_id = 'sidebar1';
$sidebars_widgets = wp_get_sidebars_widgets();
$widget_ids = $sidebars_widgets[$sidebar_id]; 
    foreach( $widget_ids as $id ) {
    echo '<li><a href="#'.$id.'">'.$id['title'].'</a></li>';
}

I'm trying to display the widget title using $id['title'], but It's not working.

  • Is your goal to only get the widgets from a specific sidebar or you just want to be able to grab widgets in general and show them? – RachieVee Dec 8 '14 at 21:49
  • The codex also says the functions you're using aren't intended to be used by developers other than core developers - that may be part of the issue: codex.wordpress.org/Function_Reference/wp_get_sidebars_widgets – RachieVee Dec 8 '14 at 21:51
  • So, do you know about any function to retrieve widget data which as stored as $instance['name']? – Ahmed Saad Dec 8 '14 at 22:10
2

Found a 'dirty' way to do it:

Here's my code:

$sidebar_id = 'sidebar1';
$sidebars_widgets = wp_get_sidebars_widgets();
$widget_ids = $sidebars_widgets[$sidebar_id]; 
    foreach( $widget_ids as $id ) {
        $wdgtvar = 'widget_'._get_widget_id_base( $id );
        $idvar = _get_widget_id_base( $id );
        $instance = get_option( $wdgtvar );
        $idbs = str_replace( $idvar.'-', '', $id );
        echo '<li><a href="#'.$id.'">'.$instance[$idbs]['title'].'</a></li>';
    }
  • Glad you solved it Ahmed! Please mark this answer as accepted so it doesn't show as an unsolved question :) – Tim Malone Jun 30 '16 at 5:13
0

If you want to just display all the widgets coming from a specific sidebar, try dynamic_sidebar. Example taken from the Codex:

<?php if ( is_active_sidebar( 'left-sidebar' ) ) : ?>
    <ul id="sidebar">
        <?php dynamic_sidebar( 'left-sidebar' ); ?>
    </ul>
<?php endif; ?>

If you're trying to get a specific widget and display it, try the_widget. This example is taken from the Categories widget:

<?php the_widget( 'WP_Widget_Links'); ?>

If neither of these help, you may have to provide us some more details as to what you're trying to do for more answers. Good luck!

  • I only want to show the title of the widget; I have the widget ID and I want to display its title. – Ahmed Saad Dec 8 '14 at 22:09
  • 3
    It looks like the_widget automatically displays the widget and you can manipulate the title with the $instance parameter, but you can't show just the title using this function. developer.wordpress.org/reference/functions/the_widget Is it that you want titles only and no widget content? Where is this being displayed? Perhaps depending on your goal, there is another way to go about it. – RachieVee Dec 9 '14 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.