Add a div to wrap a content inside a WordPress widget - php

I have created a widget in functions.php and i was able to display them on my index.php , i use the widget manager drop and drag to display the content i would like to add but i am confused how i may wrap those tags inside a div and give it a class name in order to style this specific block section.
This is functions.php
add_action('widgets_init', 'wpdevs_sidebars');
function wpdevs_sidebars(){
// Register 3 widget areas
register_sidebar(
array(
'name' => 'Service 1',
'id' => 'services-1',
'description' => '1st Service Area',
'before_widget' => '<div class="widget-wrapper-1">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widget-title-1">',
'after_title' => '</h4>'
)
);
register_sidebar(
array(
'name' => 'Service 2',
'id' => 'services-2',
'description' => '2nd Service Area',
'before_widget' => '<div class="widget-wrapper-2">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widget-title-2">',
'after_title' => '</h4>'
)
);
register_sidebar(
array(
'name' => 'Service 3',
'id' => 'services-3',
'description' => '3rd Service Area',
'before_widget' => '<div class="widget-wrapper-3">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widget-title-3">',
'after_title' => '</h4>'
)
);
}
And this is my index.php
<div class="bloc_items">
<div class="services-item-1">
<?php
if(is_active_sidebar('services-1')){
dynamic_sidebar('services-1');
}
?>
</div>
<div class="services-item-2">
<?php
if(is_active_sidebar('services-2')){
dynamic_sidebar('services-2');
}
?>
</div>
<div class="services-item-3">
<?php
if(is_active_sidebar('services-3')){
dynamic_sidebar('services-3');
}
?>
</div>
</div>

'before_widget' => '<div class="widget-wrapper-1">',
This line is already a wrapper around your widget. You can use it for styling.

Related

Widget issue - Wordpress

I have imported a theme and all is working well with Wordpress. However, I am trying to add a widget into the footer.php but for some reason this is not displaying.
My footer.php is as follows:-
<?php get_sidebar('footer_widget'); ?>
And I have added this into functions.php too
function bauhaus_arphabet_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'sidebar', 'bauhaus' ),
'id' => 'bauhaus_sidebar',
'before_widget' => '<div id="%1$s" class="widget sidebar_widget %2$s">',
'after_widget' => '</div>',
'description' => esc_html__( 'blog sidebar', 'bauhaus' ),
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Footer', 'bauhaus' ),
'id' => 'footer_widget',
'before_widget' => '<div id="%1$s" class="widget footer_widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>'
));
}
Does anyone have any idea why this is not displaying?
Thank you in advance,
Scott
Replace the code with :
<?php dynamic_sidebar( 'footer_widget' ); ?>

How To Add a Class To a Custom Sidebar in WordPress

I just learned how to create a custom WordPress theme. I would like to assign a class to my sidebar for styling, but I have not found anything online that explains how to do this. The code I added to my functions.php file to register my sidebar is:
if ( function_exists('register_sidebar') )
register_sidebar(array(
'id' => 'sidebar-1',
'description' => __( 'Add widgets here to appear in your sidebar.', 'twentysixteen' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
And the code I added to my sidebar.php file is:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
<?php endif; ?>
Any help would be greatly appreciated!
Use before_widget and after_widget to add a wrapper.
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'twentysixteen' ),
'id' => 'sidebar-1',
'description' => 'Add widgets here to appear in your sidebar.',
'before_widget' => '<aside id="%1$s" class="widget custom-class %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h2 class="widget-title custom-class">',
'after_title' => '</h2>',
) );
Try this in functions.php file

register sidebar in wordpress theme

I have a html template and I have this code in my html and wants to convert this in wordpress
<div id="home">
<!-- Home Page -->
<p class="blue">NEED A DESIGNER?</p>
<p class="orange">I AM HERE</p>
<span><i class="fa fa-phone-square"></i> +1 234 567 876 54</span>
<!-- / Home Page -->
</div>
I want to call this as dynamic_sidebar(); ....
how i can register sidebar in functions.php file...
I tried as following
add_action( 'widgets_init', 'homepage_widget' );
function homepage_widget() {
register_sidebar( array(
'name' => __( 'main homepage', 'theme-slug' ),
'id' => 'homepagelol',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'class' => 'orange',
'before_widget' => '<div id="home">',
'after_widget' => '</div>',
'before_title' => '<p class="blue">',
'after_title' => '</div>',
) );
}
There are few steps you need to follow:
Step 1 : Register your sidebar
add_action( 'widgets_init', 'theme_slug_widgets_init' );
function theme_slug_widgets_init() {
register_sidebar( array(
'name' => __( 'Main Sidebar', 'theme-slug' ),
'id' => 'sidebar-1',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
) );
}
Step 2: In widget Area, you see your sidebar, then add text widget and put your html in text widget
Step 3: Then call your sidebar in index.php
<?php dynamic_sidebar('your-sidebar-unique-id'); ?>
Then save your page and reload..
Hope this will help you.
thanks
You can try this code to register sidebar, hope it helps.
if ( function_exists('register_sidebar') )
register_sidebar(array('name'=>'MiddleSidebar',
'before_widget' => '<li class="widget">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h3>',
));
register_sidebar(array('name'=>'FooterSidebar',
'before_widget' => '<li class="widget">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h3>',
));

Blank Widgets - Custom Sidebars

I created a series of new sidebars for a custom homepage on WordPress. After saving each one, they work and continue to work. Once I return to the Widget area in the Admin area, only those widgets are blank and they are then missing from the home page. Functions.php code below for the added sidebars
// Widgetized Header
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Header',
'id' => 'Header',
'description' => 'Header Sidebar for Search',
'before_widget' => '<ul class="right"><li class="has-form">',
'after_widget' => '</li></ul>',
'before_title' => '<h4>',
'after_title' => '</h4>',
));
}
// Homepage Sidebar for Mailchimp
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Homepage Mailchimp Widget',
'id' => 'HomeWidget1',
'description' => 'Homepage mailchimp area',
'before_widget' => '<ul class="home-widget"><li class="has-form">',
'after_widget' => '</li></ul>',
'before_title' => '<h4>',
'after_title' => '</h4>',
));
}
// Homepage Sidebar for Twitter
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Homepage Twitter Widget',
'id' => 'HomeWidget2',
'description' => 'Homepage twitter area',
'before_widget' => '<ul class="home-widget"><li>',
'after_widget' => '</li></ul>',
'before_title' => '<h4>',
'after_title' => '</h4>',
));
}
// Homepage Sidebar for Promo Ad
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Homepage Promo Ad',
'id' => 'HomeWidget5',
'description' => 'Homepage Promo Ad',
'before_widget' => '<ul class="home-widget">',
'after_widget' => '</ul>',
));
}
// Homepage Sidebar for Archives
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Homepage Archive Widget',
'id' => 'HomeWidget3',
'description' => 'Homepage Widget for Archives',
'before_widget' => '<ul class="home-widget"><li class="has-form">',
'after_widget' => '</li></ul>',
'before_title' => '<h4>',
'after_title' => '</h4>',
));
}
// Homepage Sidebar for Archives
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Homepage Social Widget',
'id' => 'HomeWidget4',
'description' => 'Homepage Widget for Social Nav',
'before_widget' => '<ul class="home-widget"><li>',
'after_widget' => '</li></ul>',
'before_title' => '<h4>',
'after_title' => '</h4>',
));
}
There are a few things that could be causing that problem.
1) register_sidebar() doesn't need to be wrapped in a conditional here. It's a WordPress core function, so it ain't going anywhere. This will do just fine:
register_sidebar(array(
'name' => 'Homepage Twitter Widget',
'id' => 'HomeWidget2',
'description' => 'Homepage twitter area',
'before_widget' => '<ul class="home-widget"><li>',
'after_widget' => '</li></ul>',
'before_title' => '<h4>',
'after_title' => '</h4>',
));
2) Use action hook widgets_init to load the registrations. For multiple instances of register_sidebar, I would recommend wrapping all instances in a function, and then hooking that function. Ex:
function myPrefix_widgets_init() {
register_sidebar(array(
'name' => 'Homepage Mailchimp Widget',
'id' => 'HomeWidget1',
'description' => 'Homepage mailchimp area',
'before_widget' => '<ul class="home-widget"><li class="has-form">',
'after_widget' => '</li></ul>',
'before_title' => '<h4>',
'after_title' => '</h4>',
));
// Register the rest of the widgets here.
}
add_action( 'widgets_init', 'myPrefix_widgets_init' ); //<-- This is the action hook
This post from Justin Tadlock is a few years old now, but still fantastic at explaining in detail how WordPress wants to handle widgetized sidebars: http://justintadlock.com/archives/2010/11/08/sidebars-in-wordpress

Create widget area in wordpress dashboard pages section?

How to create a widget area in wordpress pages section. if I add below code in my functions.php file the widget created in dashboard menu section I want to add additional widget in my pages section not in dashboard.
Add a widget to the dashboard.
//This function is hooked into the 'wp_dashboard_setup' action below.
function example_add_dashboard_widgets() {
wp_add_dashboard_widget(
'example_dashboard_widget', // Widget slug.
'Example Dashboard Widget', // Title.
'example_dashboard_widget_function' // Display function.
);
}
add_action( 'wp_dashboard_setup', 'example_add_dashboard_widgets' );
Create the function to output the contents of our Dashboard Widget.
function example_dashboard_widget_function() {
// Display whatever it is you want to show.
echo "Hello World, I'm a great Dashboard Widget";
}
you should register sidebar in functions.php as
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'widget_name',
'id' => 'widget-id',
'description' => 'Type something here',
'before_widget' => '<div id="one" class="two">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
it will register sidebar(widget) in your dashboard. Now you can you is it anywhere by
<?php dynamic_sidebar('widget-id'); ?>
This function already exists in default theme
function twentyfourteen_widgets_init() {
require get_template_directory() . '/inc/widgets.php';
register_widget( 'Twenty_Fourteen_Ephemera_Widget' );
register_sidebar( array(
'name' => __( 'Primary Sidebar', 'twentyfourteen' ),
'id' => 'sidebar-1',
'description' => __( 'Main sidebar that appears on the left.', 'twentyfourteen' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h1 class="widget-title">',
'after_title' => '</h1>',
) );
register_sidebar( array(
'name' => __( 'Content Sidebar', 'twentyfourteen' ),
'id' => 'sidebar-2',
'description' => __( 'Additional sidebar that appears on the right.', 'twentyfourteen' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h1 class="widget-title">',
'after_title' => '</h1>',
) );
register_sidebar( array(
'name' => __( 'Footer Widget Area', 'twentyfourteen' ),
'id' => 'sidebar-3',
'description' => __( 'Appears in the footer section of the site.', 'twentyfourteen' ),
'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', 'twentyfourteen_widgets_init' );
you can create multiple widget area by copy and pasting this and chage the id every time
register_sidebar( array(
'name' => __( 'new area', 'twentyfourteen' ),
'id' => 'sidebar-5',
'description' => __( 'Appears in the footer section of the site.', 'twentyfourteen' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h1 class="widget-title">',
'after_title' => '</h1>',
) );
}
In function.php add:
function create_widget($name, $id, $description) {
register_sidebar(array(
'name' => __( $name ),
'id' => $id,
'description' => __( $description ),
'before_widget' => '<div id="'.$id.'" class="widget %1$s %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>'
));
}
create_widget("Header", "uptop", "Displays in the header of the site, above the title"); // Create the actual widgets
Then add next code in place u wanna use this area, for example in your header.php:
<?php if ( !dynamic_sidebar('uptop') ); ?>

Categories