How to display my WordPress widget programmatically?

I need to display my custom widget in a WP based custom page template.

This is the scenario:

I created a page based on a custom page template called "product", and now I need my custom widget to appear on its sidebar. I need this sidebar to be completely different from other pages, so I want to display it programmatically. I tried to use the_widget () function, but I think that it only works with built-in widgets, and I also don’t know how to pass parameters registered with the register_sidebar function to it, because it seems that they do not use them by default.

Actually I used this: the_widget ('WP_Widget_Search') for testing, and the widget was there, but it ignored the theme settings, I mean the settings in function.php:

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

Therefore, as I said, I do not know if it works with custom widgets (created in plugins), because I do not know how to pass the widget name and parameters. How to do it?

+4
source share
4 answers

Dynamically create a dynamic instance of a widget using the_widget(), for example:

<div id="xxx" class="widget-area" role="complementary">
    <div id="sidebar-dyn">
        <?php the_widget( 'WP_Widget_Recent_Posts' ); ?>
    </div>
</div>
+7
source

Why do I need to program a software widget. I mean, you can just create another sidebar that only appears on your custom product page template

, , - . "prdouct" ( ).

<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Product Sidebar')) ;?>

.

+1

, , :

<?php echo do_shortcode( $content ) ?>
+1

, , , , 1. 2. , .

, functions.php( ):

<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name'=> 'Product Sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div></div>',
'before_title' => '<h3>',
'after_title' => '</h3><div class="padder">'
));
?>

sidebar.php, "".

<?php if (is_page('Product')) ;?>
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Product Sidebar')) ;?>
<?php endif; ?>
0

All Articles