0
votes

I am new to WordPress. I am creating a custom sidebar where I want to display only Text Widget on a Contact page. There will be different sidebar on other pages.

I know how to make different sidebars on different pages.

The issue is: I want to display specific text widget on the custom sidebar. How do I target specific text bar as well as how to pass its value on the code?

I tried to create text widget using

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

What will be the instance and args in this code?

P.S: I am using Zerif-lite wordPress theme

1

1 Answers

0
votes

I think you are misunderstanding the use of the_widget function. The description from the WordPress Codex article is:

This template tag displays an arbitrary widget outside of a sidebar.

It shouldn't be used in a custom sidebar created with register sidebar.

If you just have a sidebar layout, without a registered WordPress sidebar, then you should use the_widget.

The second parameter, $instance, is an array or query string of the widgets settings.

For example, to display the WordPress categories widget in a template you would use:

$instance = array(
    'title'        => __( 'Categories' ),
    'count'        => 0,
    'hierarchical' => 0,
    'dropdown'     => 0,
);

the_widget( 'WP_Widget_Categories', $instance );

If you are using a custom sidebar for the contact form you can use get_sidebar to display it in your template.

<?php get_sidebar('contact'); ?>

This would pull from the file wp-content/yourTheme/sidebar-contact.php. That sidebar can use the_widget directly to display your text widget or use dynamic_sidebar to display widgets added to the sidebar in the WordPress dashboard.