I'm currently coding my own one page wordpress theme (based on underscores) and I'm trying to add a widget to a page, so this widget will show up on my homepage.
I used this page as my guide; http://codex.wordpress.org/Widgetizing_Themes but the widget is not showing up on my homepage.
Steps I took:
1. Create a custom page template, this is the code of that:
<?php
/*
Template Name: Widget Page
*/
get_header(); ?>
<div id="primary" class="content-area">
<?php putRevSlider("video","homepage") ?>
<main id="main" class="site-main" role="main">
<?php dynamic_sidebar( 'widget-page' ); ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Seems to work fine, I can select the template and the content of my page shows up on the homepage. (See test widget, test test test)
I added the line that calls on the widget there too, as you can tell.
Then I added this to my functions.php;
/**
* Register our sidebars and widgetized areas.
*
*/
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Widget Page',
'id' => 'widget-page',
'before_widget' => '<div>',
'after_widget' => '</div>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
And I think that's where it's going wrong? The mentioning of a sidebar confuses me a little as this is supposed to show up on a page. The wp guide also mentions a code where it checks if the sidebar is active, but since I'm not putting it in a sidebar I'm not sure if I should use this? I tried using this piece but that also didn't work, which is why I think the problem would be in the functions.php file. The underscores framework also comes with their own piece of widget code, is this causing problems?
/**
* Register widget area.
*
* #link http://codex.wordpress.org/Function_Reference/register_sidebar
*/
function otto_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'otto' ),
'id' => 'sidebar-1',
'description' => '',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h1 class="widget-title">',
'after_title' => '</h1>',
) );
}
add_action( 'widgets_init', 'otto_widgets_init' );
This is a link to my page; http://kellyvuijst.nl/onepage/
Thanks in advcance!
After some more googling I found out the easiest option to make a widget appear in a page is to make sure you can short code widgets and then just add the short code to the page. No custom templates needed.
This is the code I used in my functions.php;
function widget($atts) {
global $wp_widget_factory;
extract(shortcode_atts(array(
'widget_name' => FALSE
), $atts));
$widget_name = wp_specialchars($widget_name);
if (!is_a($wp_widget_factory->widgets[$widget_name], 'WP_Widget')):
$wp_class = 'WP_Widget_'.ucwords(strtolower($class));
if (!is_a($wp_widget_factory->widgets[$wp_class], 'WP_Widget')):
return '<p>'.sprintf(__("%s: Widget class not found. Make sure this widget exists and the class name is correct"),'<strong>'.$class.'</strong>').'</p>';
else:
$class = $wp_class;
endif;
endif;
ob_start();
the_widget($widget_name, $instance, array('widget_id'=>'arbitrary-instance-'.$id,
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => ''
));
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode('widget','widget');
Which I found thanks to; https://digwp.com/2010/04/call-widget-with-shortcode/
Problem solved!
Related
I've created a custom widget area in the plugin I'm creating.
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Home right sidebar',
'id' => 'home_right_1',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
dynamic_sidebar( 'home_right_1' );
And to enable showing the widgets stored in this widget I have inserted this code inside the footer.php of the theme I'm currently using for testing.
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-
area" role="complementary">
<?php dynamic_sidebar( 'home_right_1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
It works, when I add widgets to my widget area, they are shown in footer of the website. But I want to make the plugin works without changing the footer.php manually. How can I check which theme is activated and then find that theme, the footer.php file and add that code exacltly where I want (to be shown below any other footer created with the theme when is installed. Also, I would like to remove the footer that was generated before.
i am working on a simple WordPress blog and i am trying to display a widget that appears in every page in the website .. i have followed the dynamic sidebar widget approach, but the problem ,
here is the Function.php file
function wpb_widgets_init() {
register_sidebar( array(
'name' => 'Custom Header Widget',
'id' => 'custom-header-widget',
'before_widget' => '<div class="chw-widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="chw-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'wpb_widgets_init' );
and my header.php file
<?php if ( is_active_sidebar( 'custom-header-widget' ) ) : ?>
<div id="header-widget-area" class="chw-widget-area widget-area" role="complementary">
<?php dynamic_sidebar( 'custom-header-widget' ); ?>
</div>
<?php endif; ?>
and this works fine only in any single post.
but in the home page/or any template file nothing shows !!!
Any ideas ?
You can try this structure and make sure your sidebar active
I have figured it out
i hade to create a template file for my new sideBar called: sidebar-.php
and use the get_sidebar(id) method to call it
Voilet it works in all templates ...
I have a calendar plugin I wish to use, I tested it first on the WP '17 default theme and it displays just fine. I have my own custom theme I'm building using w3.css, very basic stuff.
I created a display area for widgets using
<?php
/**
* Register our sidebars and widgetized areas.
*
*/
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Home right sidebar',
'id' => 'home_right_1',
'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', 'arphabet_widgets_init' );
add_theme_support( 'customize-selective-refresh-widgets' );
wp_enqueue_script("jquery");
?>
And put the following in an appropriate div within my index.php:
<?php dynamic_sidebar( 'home_right_1' ); ?>
As far as I can tell, when my "theme" outputs the widget there is no stylesheet attached to it. It's a calendar widget and displays the entire list of days of the month in a long list, rather than an enclosed area.
Any suggestions would be appreciated.
Okay, I hadn't called the footer which is apparently where the function hooks to. Now it's working as expected.
<?php get_footer(); ?>
(Placed just before </body> in the index file.)
I also ended up using the following to call it:
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'home_right_1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
my footer shows in the normal page fine. I created a new post template and have this in the footer:
<?php if ( !function_exists('dynamic_sidebar') || dynamic_sidebar('sidebar-2'))
get_footer();
Like this my widgets show fine. In my functions file I have this:
add_action( 'widgets_init', 'child_register_sidebar' );
function child_register_sidebar(){ register_sidebar(array(
'name' => 'Sidebar 2 for single_withnotes',
'id' => 'sidebar-2',
'description' => '',
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<span class="widget-title">',
'after_title' => '</span>',
));
}
and my new sidebar look like this:
if ( ! is_active_sidebar( 'sidebar-2' ) ) {
return;
}
?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-2' ); ?>
</div><!-- #secondary -->
Can anyone suggest why this is? Thanks.
In your code, your footer only shows when the "if sentence" is right. dynamic_sidebar("sidebar-2") only returns true when widget "sidebar-2" exists and it has content.
Make sure you have added content to the sidebar-2 in Appearance> Widgets.
Also you should use "is_active_sidebar" in an "if sentence", because dynamic_sidebar sends content to output buffer.
If you have content in "Sidebar 2...", other possibility is that there are an error in some of the widgets linked to that position.
Put at the begining of your file:
error_reporting(E_ALL);
ini_set('display_errors', 1);
And watch if some errors are showned in the page.
Also you can remove all content from Sidebar 2 and tried only with some text.
I followed the instructions here for how to add a widget to a template. I added this code to functions.php:
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Carousel',
'id' => 'carousel_widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
and I added this to my custom template:
<?php if ( is_active_sidebar( 'carousel_widget' ) ) : ?>
<div id="carousel-widget" class="carousel widget-area">
<?php dynamic_sidebar( 'carousel_widget' ); ?>
</div>
<?php endif; ?>
It worked, but my new widget showed up as a sidebar, and I want it to be in the main body of the page. Obviously this is because of the dynamic_sidebar function, but the documentation is not very helpful on how to place a widget on the page NOT as a sidebar.
I tried replaced the php with this:
<div id="carousel-widget" class="carousel widget-area">
<?php the_widget( 'carousel_widget' ); ?>
</div>
But now it doesn't show up at all.
How do I place this widget on my page without it showing up as an absolutely positioned sidebar?