5

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?

2
  • 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 Commented Feb 4, 2015 at 20:15
  • Had this quesiton been resolved? Commented Aug 31, 2016 at 18:42

5 Answers 5

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.

2
  • 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
    Commented Feb 4, 2015 at 18:52
  • @Bergmann, you can create as many widgets and widget areas as you want.
    – luukvhoudt
    Commented Feb 4, 2015 at 18:56
3

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

2

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' ); ?>

2
  • how would I find out the name of a widget? I'm using the Rey Theme and it offers a widget to display products. I'm writing custom code and want to include it but don't know the name of the widget
    – ekkis
    Commented Aug 14, 2020 at 2:06
  • look at the code inside.
    – Alexufo
    Commented Aug 15, 2020 at 9:57
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" ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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