I want to remove main sidebar from dwqa-question page and want to add custom sidebar in that page.
I wrote a code for it in functions.php :
function dwqa_theme_register_sidebar() {
register_sidebar( array(
'name' => __( 'Single Question', 'multinews' ),
'id' => 'dqwa',
'class' => '',
'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', 'dwqa_theme_register_sidebar' );
function remove_main_sidebar_dwqa_question(){
if ( is_singular('dwqa-question') ){
unregister_sidebar( 'Main Sidebar' );
}
}
add_action( 'widget_init', 'remove_main_sidebar_dwqa_question' );
And this code in page.php :
<?php if ( is_singular('dwqa-question') ): ?>
<?php dynamic_sidebar('dqwa') ?>
<?php endif; ?>
Below is the output screenshot :
single question page
I think you dont need to de-register the main sidebar every time, it will take time to render the output. Just write the code in siderbar.php. Here I am mixing some of your code with.
<?php
if ( is_singular('dwqa-question') )
{
dynamic_sidebar('dqwa');
}
else
{
/** FOR OTHER THAN DWQA ***/
dynamic_sidebar('main-sidebar'); //I might be wrong on ID
}//end if
?>
Hope it will work for you
Related
I'm trying to display a custom sidebar in a page. The code works with custom post types "news" posts but wont work with the page "na-midia". In the page the defaul sidebar is shown.
// Custom Sidebar
function prefix_custom_sidebar() {
register_sidebar( array(
'name' => __( 'Custom Sidebar MÃdia', 'page-builder-framework' ),
'id' => 'custom-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="wpbf-widgettitle">',
'after_title' => '</h4>'
) );
}
add_action( 'widgets_init', 'prefix_custom_sidebar' );
// Replace the default sidebar with our new custom sidebar on all docs posts
function prefix_do_custom_sidebar( $sidebar ) {
// this statement works for custom post types
if( is_singular( 'news' ) ) {
$sidebar ='custom-sidebar';
}
// this statement NOT works for the page which is displaying the posts, display the default sidebar instead
elseif ( is_singular( 'na-midia' ) ) {
$sidebar ='custom-sidebar';
}
return $sidebar;
}
add_filter( 'wpbf_do_sidebar', 'prefix_do_custom_sidebar' );
Solved!
elseif ( is_page ( 'na-midia' ) ) {
$sidebar ='custom-sidebar';
}
I can't create left sidebar.
I created a file into child theme folder: sidebar-left.php:
<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly ?>
<aside class="sidebar">
<!-- SIDEBAR WIDGET AREA -->
<?php if ( is_active_sidebar( 'lhsidebar' ) ) : ?>
<?php dynamic_sidebar( 'lhsidebar' ); ?>
<?php else : ?>
<p><?php esc_html_e('No widgets added', 'rehub-theme'); ?></p>
<?php endif; ?>
</aside>
Then, I have added this code into child theme's function.php:
// Left SideBar
register_sidebar( array(
'name' => esc_html__( 'Left Sidebar', 'rehub' ),
'id' => 'lhsidebar',
'description' => esc_html__( 'Add widgets here.', 'rehub' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
But where should I put the get_sidebar('sidebar-left')?
You have to call the sidebar inside any template of your child theme either by using the get_sidebar() function:
<?php get_sidebar('left'); ?>
Note: the name of the file without sidebar- part at the begin of the name as mentionned in the doc.
Either by using the get_template_part() function if your template file is not in the same directory as the sidebar file.
<?php get_template_part( 'sidebar-templates/sidebar', 'left' ); ?>
Note: here the full name of the sidebar is mentionned in the function, as well as its path from the root of the child-theme, cf doc.
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?
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!
I'm trying to implement a custom sidebar to my custom wordpress template.
I added this to my functions.php:
if ( function_exists('register_sidebar'))
register_sidebar(array(
'name' => 'NiceBar',
'id' => 'sidebar-widget',
'before_widget' => '<li>',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
This to my sidebar.php:
<ul class="sidebar">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('NiceBar') ) : ?>
<?php endif; ?>
</ul>
And this to my index.php:
<?php get_sidebar('NiceBar'); ?>
Now the result is, though I am getting a sidebar it's just a default one and not my custom 'NiceBar' on the frontend. In the wordpress backend the 'NiceBar' is registered and I can and remove stuff to it but it hast just no influence on my website.
Any kind of advice is very welcome
You're referencing your sidebar by name, instead you should get it via id :
<?php get_sidebar('sidebar-widget'); ?>
This should work:
functions.php
if ( function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'NiceBar',
'id' => 'sidebar-widget',
'before_widget' => '<li>',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
}
sidebar.php
<ul class="sidebar">
<?php if ( function_exists('dynamic_sidebar')) {
dynamic_sidebar('sidebar-widget');
} ?>
</ul>
index.php
<?php get_sidebar(); ?>
Problem is in the get_sidebar() function you are telling that in your theme there is a file called: sidebar-sidebar-widget.php because you are passing argument sidebar-widget in the get_sidebar() function. read it here