WordPress.org

Ready to get started?Download WordPress

Forums

calling a widget from anywhere in a theme (7 posts)

  1. dans7919
    Member
    Posted 4 years ago #

    So we can call any widget anywhere in a theme using the_widget. Bono has a page about the_widget here

    We can call the default pages widget like this:

    the_widget('WP_Widget_Pages');

    We can custom wrap it like this:

    $args = array(
        'before_widget' => '<div class="widget">',
        'after_widget'  => '</div></div>',
        'before_title'  => '<div class="widgetTittle">',
        'after_title'   => '</div><div class="widgetBody">' );
    the_widget('WP_Widget_Pages', null, $args);

    We can even tell it to custom sort it like so:

    the_widget('WP_Widget_Pages', 'sortby=post_modified', $args);

    But for the love of god, does anyone know how to get access to the stored widget options? I'm going out of my mind trying to figure it out.

    -Dan

  2. dans7919
    Member
    Posted 4 years ago #

    Bump

  3. yenerich
    Member
    Posted 4 years ago #

    This work only for generic widgets?
    If i install some widget, how can i call it from anywhere?

    Also, is there a way to obtain the list of widgets on any sidebar from code?

    Thanks in advance

  4. -JFK-
    Member
    Posted 4 years ago #

    Also, looking for an answer on this one.

    I will continue to research and report if I find anything.

  5. Rev. Voodoo
    Volunteer Moderator
    Posted 4 years ago #

    do you mean from within posts? Or anywhere in your template? You can just add more widget areas anywhere in your theme really.

    register_sidebar(array(
    	'name'=>'moreWidgets',
            'before_widget' => '<div id="%1$s" class="widget %2$s">',
            'after_widget' => '</div>',
            'before_title' => '<h3>',
            'after_title' => '</h3>',
        ));

    in functions.php, (within the <?php of course) creates another 'sidebar' that is a widget area

    <div class="widgetBox">
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('moreWidgets') ) : ?>
    <h3>Archives</h3><p>All entries, chronologically...</p><ul><?php wp_get_archives('type=monthly&limit=12'); ?> </ul>
    <?php endif; ?>
    </div>

    Can be dropped in any template where you want a widget. Then add .widgetBox (of course you'll need to change the class name on a per widget area) to your css and style the area. This is set up to display Archives as default, which goes away when you add a widget.

  6. -JFK-
    Member
    Posted 4 years ago #

    RVoodoo... My research seems to keep yielding results similar to what you have written.

    I was able to make a call to my custom Widget from anywhere within my Theme with the following code:

    <?php $track = new Tracking();
          echo $track->widget($args, $instance); ?>

    My custom widget is "Tracking" and it extends "WP_Widget".

    The problem with that solution is... the user settings that are made in the Admin Panel for my widget are NOT being returned in my custom widget call (above). I am guessing due to the fact I am instantiating a new Tracking object.

    I'll try what you suggested and report back. I just didn't want to have to write anything additional into the functions.php file. That reasoning does make sense if I told you the overall picture of what I am trying to accomplish.

  7. -JFK-
    Member
    Posted 4 years ago #

    Ok. Hats off to RVoodoo for urging me towards working solution.

    Wasn't how I initially planned on doing it, but it worked.

    I was trying a custom written implementation of "the_widget() function" that is listed on this website:

    http://core.trac.wordpress.org/ticket/9701

    But, that didn't work for me. I couldn't even Activate their demo plugin. WordPress said there was an error when I tried.

    Thanks again. That was the first response I believe I have recieved for any of my question posts.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags