I need to display my custom made widget in WP page based custom page template programmatically.
This is the scenario:
I created page based on custom page template called "product" now I need in its sidebar display my custom made widget. I need this side bar to be totally different from other pages this is why I want to display it programmatically.
I tried to use the_widget() function but I think it works only with built in widgets and also I don't know how to pass parameters registered with register_sidebar function to it, because it seems it doesn't use them by default.
Actually I used this : the_widget('WP_Widget_Search') to test it and widget was there but it ignored theme settings, I mean settings in function.php :
register_sidebar( array(
'name' => 'Sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div></div>',
'before_title' => '<h3>',
'after_title' => '</h3><div class="padder">'
) );
So as I said I don't know if it works with custom made widgets (created in plugins) because I don't know how to pass widget name there and params. How to do that?
Dynamically create a dynamic instance of the widget using the_widget(), eg:
<div id="xxx" class="widget-area" role="complementary">
<div id="sidebar-dyn">
<?php the_widget( 'WP_Widget_Recent_Posts' ); ?>
</div>
</div>
Why the need to include custom widget programatically. I mean you can simply create another sidebar that is shown only on your custom page template "product"
Further as you need to do it for your custom page, you don't even need to check for any condition. Simply put this code in your "prdouct" page template file anywhere ( where you would like to display your sidebar".
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Product Sidebar')) ;?>
And simply drag your widget to this sidebar from admin panel.
If the widget has a shortcode, call it using this:
<?php echo do_shortcode( $content ) ?>
you're question isn't exactly clear to me, but what i am gathering is that
1. you need to register your widget and
2. you need for it to ONLY show on the product page.
to register, place this in functions.php (you may already have the first line in your functions file):
<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name'=> 'Product Sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div></div>',
'before_title' => '<h3>',
'after_title' => '</h3><div class="padder">'
));
?>
this would be the code to include in sidebar.php to display the above only on the page called "Product".
<?php if (is_page('Product')) ;?>
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Product Sidebar')) ;?>
<?php endif; ?>
Related
I'm creating a custom theme and in my contact page i want to add a widget to display google maps map throught Wp Google Map plugin but i can't show the map in my page.
In my functions.php i have this:
function arphabet_widgets_init() {
register_sidebar(array(
'name' => 'My_Widgtet_Area',
'id' => 'map-area',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
));
}
In my html file:
<?php
$map = get_page_by_title( 'Mapa' );
echo $map->post_content;
?>
In my page content:
[wpgmza id="1"]
How can i display or associate the widget to my page?
Thank's
You can add the map directly using the given shortcode or you can add the shortcode in the widget and add the widget in page.
To add the map directly in the page using shortcode
echo do_shortcode('[wpgmza id="1"]');
To add widget in the page
dynamic_sidebar('map-area');
Hope it helps...
Please ask if you have any doubts.
Please could someone show me how to do this? My theme (Fable) does not have a built-in sidebar and I need one for my homepage.
Many thanks!
Create a child theme.
Create a file sidebar.php.
In your sidebar.php-file you have to use at least the following function: dynamic_sidebar()
Furthermore in your functions.php file of your child Theme you must use the following functions: register_sidebar()
Include the sidebar in your child theme (most probably in the overwritten index.php-file)
Style the sidebar to integrate it with your Theme properly.
For further documentation see the WordPress Codex:
https://codex.wordpress.org/Child_Themes
https://codex.wordpress.org/Function_Reference/dynamic_sidebar
https://codex.wordpress.org/Function_Reference/register_sidebar
Put this in your functions.php file:
<?php
// Declare sidebar widget zone
register_sidebars( 1,
array(
'name' => 'My Widget Area',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>'
)
);
?>
Then put this in the place in your theme where you want the widget area to appear:
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('My Widget Area')) : else : ?><?php endif; ?>
I am creating a wordpress theme from scratch.
I created my own page.php, header.php , footer.php, sinngle.php .
When i install any 3rd party plugin , it does not work but works on any third party themes.
Do i need to include any predefined wordpress API to make it work ?
Yes, there are several functions you need to add in your theme to make fullfledged WordPress theme.
First read this thoroughly. https://codex.wordpress.org/Theme_Development
Some important points:
In header.php, there should be wp_head() function call just before </head>
In footer.php, there should be wp_footer() function call just before </body>
You need to use WordPress loop properly. https://codex.wordpress.org/The_Loop
If you want to register Navigation, Sidebar, etc then it should be done in functions.php
Display your widget in your theme pages like this:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('header_sidebar') ) :
endif; ?>
You of course can replace the dynamic_sidebar above with the actual tag name of the 3rd-party widget you wish to integrate.
If you make your own widgets, register them in your functions.php
http://wpgyan.com/how-to-create-a-widget-area-in-wordpress-theme/
Example:
<?php
/**
* Register Widget Area.
*
*/
function wpgyan_widgets_init() {
register_sidebar( array(
'name' => 'Header Sidebar',
'id' => 'header_sidebar',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'wpgyan_widgets_init' );
?>
I'm following multiple tutorials that all tell me to put this into my functions.php to add a widget area:
<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '',
'after_widget' => '',
'before_title' => '<div class="title">',
'after_title' => '</div>',
));
?>
Once I upload that file and try to change anything, like post or edit a post I get a white screen directly after hitting publish or any other action button.
Is there something wrong with that code? Or am I missing something when it comes to adding widget areas to my template?
Wordpress doesn't like blank lines on the functions.php page..
How can i add a custom widget to wp-admin/post-new.php?
Some themes use it, and i wan't to be able to do the same thing in my theme :)
Thanks
/Richard
You can create yourself a new side bar in functions.php, copy and paste one of the current ones, eg:
register_sidebar( array (
'name' => 'Primary Widget Area',
'id' => 'primary_widget_area',
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => "</li>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
Change any references to primary to a name of your choice maybe post-new
Then add this side bar to wp-admin/edit-form-advanced.php. Search for 'side-info-column'. Just before that div closes ad in the call to the new side bar:
<?php if ( is_sidebar_active('secondary_widget_area') ) : ?>
<div id="secondary" class="widget-area">
<ul class="xoxo">
<?php dynamic_sidebar('secondary_widget_area'); ?>
</ul>
</div><!-- #secondary .widget-area -->
<?php endif; ?>
Again, you can copy one of the current ones found in sidebar.php.
You can then go to dashboard > Appearance > Widgets and you should see your new sidebar there.
Hope that helps. I'll be happy to help more if needs be.
Found what I was looking for. Its called meta boxes and there are lots of tutorials how to implement them. Just Google it ;)