2

I've a custom widget for ads, it helps me to show ads on sidebar but I'm trying to display it under every single post or on front page near to slider.

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

How could I call custom widget anywhere I want?

  • You can filter the_content and return it after the content and use a conditional tag is_front_page(); OR you can call the sidebar in your single.php or content.php file – Brad Dalton Feb 4 '15 at 20:15
  • Had this quesiton been resolved? – Ethan Jinks O'Sullivan Aug 31 '16 at 18:42
4

Add a widget area to your single.php, front-page.php, home.php or whatever you want.. template's theme file. Than at the back-end go to Appearance > Widgets and move the widget to the widget area you just created.

  • now another issue sticked to my mind, was it possible to display 5 ads using one widget but for different placement? or did i need to use one widget for one ads. Total i'll have to create 5 widgets for my ads – Dragut Feb 4 '15 at 18:52
  • @Bergmann, you can create as many widgets and widget areas as you want. – Fleuv Feb 4 '15 at 18:56
2

We can't display custom widget directly in content or any other place. the_widget() function only display built-in widget.

But still if you want display custom widget then you must create sidebar and assign widget to newly created sidebar and display that sidebar using dynamic_sidebar( 'id' );.

For more information http://codex.wordpress.org/Widgetizing_Themes

1

@Bergmann,It is possible to display 5 ads in single page using single widget "if all ads are together" . But if you want all ads at different places , you have to create different widget areas and different widgets .

1

A clean, simple, and easy solution to your question is to look into the amr shortcode any widget. As the name suggests, you can insert your widgets in your pages by using shortcodes, like so:

[do_widget "Your Widget" ]
1

Custom widget works well with <?php the_widget( '' ); ?>

You can test it.

  1. Copy widget example https://codex.wordpress.org/Widgets_API#Displaying_Widgets

  2. Don't forget register as say in codex add_action( 'widgets_init', ...

  3. Call it from index.php <?php the_widget( 'Foo_Widget' ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.