• Resolved pmendola

    (@pmendola)


    Hi, I’m trying to change the location of the widgets on my home page. So I made a new <section> and am trying to call a custom html widget there, instead of just typing it up in html inside index.php. I am able to call the wordpress built in widgets with “the_widget(‘WP_xxx”); but I can’t seem to figure out how to call on the ones I made, specifically one with an ID custom_html-2.

    I’m using the underscores theme and they have the register sidebar function in the code but I think I still need to give a class to the custom widgets to be able to call it ? and not sure how to apply that.

    It seems simple but I’m a bit stuck and can’t really find any similar questions

    function peto_theme_widgets_init() {
    	register_sidebar( array(
    		'name'          => esc_html__( 'Sidebar', 'peto_theme' ),
    		'id'            => 'sidebar-1',
    		'description'   => esc_html__( 'Add widgets here.', 'peto_theme' ),
    		'before_widget' => '<section id="%1$s" class="widget %2$s">',
    		'after_widget'  => '</section>',
    		'before_title'  => '<h2 class="widget-title">',
    		'after_title'   => '</h2>',
    	) );
    }
    add_action( 'widgets_init', 'peto_theme_widgets_init' );

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Themes don’t output specific widgets. Themes output widget areas, which can contain any number of widgets, including zero.
    The index.php template file only handles the home page if there is not some other template file, and that goes for other pages also. It is the fallback, so it has to work for any case, not just the home page.
    If you want custom HTML in your home page, just put it in your home Page, in the editor. If you need something dynamic, use the new editor, or use a plugin that supplies the dynamic thing you want. (There is a plugin that displays a widget using a shortcode: https://wordpress.org/plugins/amr-shortcode-any-widget/)

    You do not need to use the theme provided function name if that is what ‘peto’ is.

    Next, see this recently answered topic:

    https://wordpress.org/support/topic/how-to-add-custom-widget-area-to-woocommerce-ccategory-page/

    I would also wrap the php added with some custom div and/or other HTML so you can style it uniquely.

    With that, as Joy noted, you will have a custom widget area you can add custom widgets to.

    You can use the same function to init multiple widget areas, just give them unique names and ids. Then just pull that id in php.

    Thread Starter pmendola

    (@pmendola)

    thanks for the help! this is probably going to be static, so I guess I will just add the html and style it as I need for now, I just figured best practice would be to use the widget I already made in the WP admin so I don’t have to go into the theme editor to change the text. which, that link you posted helps a lot with that, thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding custom html widget anywhere on page’ is closed to new replies.