Satch3000
Satch3000

Reputation: 49384

Wordpress Template. Display contents of a Text Widget

I have created a wordpress template.

I have added some HTML into a Text widget and saved it.

Is there a way to add some code into my template so I can get that widget to be displayed?

Is this possible instead of having to enter the html code directly into my template?

Upvotes: 2

Views: 780

Answers (2)

Alex
Alex

Reputation: 132

Yes it is infact it's very simple, you have to create a sidebar for where you wan that widget to go. In your main php file or wherever you want that widget to appear you can add the following code:

 <?php if ( !function_exists('dynamic_sidebar')
    || !dynamic_sidebar('sidebar1') ) : ?>
    <?php endif; ?>

Then you would also have to declare that sidebar within the functions.php file like so:

if ( function_exists('register_sidebar') )
register_sidebar(array('name'=>'sidebar1',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));

and you can have as many sidebars that you want, just give them a different name like sidebar2 etc..

Upvotes: 1

Jonathan
Jonathan

Reputation: 6537

Your Text widget will be inside of a sidebar. So if you have a sidebar called "left-sidebar", you can use code like this in your template.

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

Now all the widgets you put in left-sidebar will be included in that template automatically.

There's more information about dynamic_sidebar here: https://codex.wordpress.org/Function_Reference/dynamic_sidebar

Note that you can also create new sidebars using register_sidebar in your functions.php, so if you want to use different sidebars in different templates you can. https://codex.wordpress.org/Function_Reference/register_sidebar

Upvotes: 2

Related Questions