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';
}
Related
I've registered a new widget area in functions.php using
'name' => __( 'Grand angle sous les articles', 'twentyten' ),
'id' => 'grand-angle',
'description' => __( 'Grand angle en bas des articles', 'twentyten' ),
'before_widget' => '<div id="grand-angle">',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => '',
) );
I'm calling this new widget area in a php template like this :
<?php if ( is_active_sidebar( 'grand-angle' ) ) : dynamic_sidebar( 'grand-angle' ); endif; ?>
Inside this new widget area I'm now adding several custom html widgets and I would like to display them randomly. These custom html widgets contain ads (mostly images) and I need to set up a rotation. Any idea how to do that? Thank you very much for your help.
The solution I've found :
add_filter( 'sidebars_widgets', 'custom_randomize_widget_order' );
function custom_randomize_widget_order( $sidebars_widgets ) {
$sidebars = array( 'grand-angle' ); // set the ids of your sidebars whose widgets you want to shuffle
foreach ( $sidebars as $sidebar ) {
if ( isset( $sidebars_widgets[ $sidebar ] ) && ! is_admin() ) {
shuffle( $sidebars_widgets[ $sidebar ] );
// Slice the array:
$sidebars_widgets[ $sidebar ] = array_slice( $sidebars_widgets[ $sidebar ], 0, 1 );
}
}
return $sidebars_widgets;
}
I am using Wordpress and PHP. I declared a custom sidebar using this code :
function customtheme_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'My custom sidebar', 'customtheme' ),
'id' => 'my-custom-sidebar',
'description' => esc_html__( '.', 'customtheme' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
add_action( 'widgets_init', 'customtheme_widgets_init' );
Ok so In my code I want to get my sidebar and store it in a PHP variable $the_widget. Using this code :
if ( is_active_sidebar( 'my-custom-sidebar' ) ) :
$the_widget = dynamic_sidebar('my-custom-sidebar');
endif;
when i do that it automatically echoes the widget(sidebar) when i call dynamic_sidebar() can i do that without the echo?? is there another function in the wordpress codex that can do the same?
No unfortunately there isn't a WordPress function such as get_dynamic_sidebar() however a common method for getting the sidebar into a variable as a string is this:
if ( is_active_sidebar( 'my-custom-sidebar' ) ) :
ob_start();
dynamic_sidebar('my-custom-sidebar');
$the_widget = ob_get_contents(); //or ob_get_clean();
ob_end_clean();
endif;
In my Genesis Magazine pro theme I'm trying to add a widget area between the header and the content. What I'm trying to do is perfectly described here: https://wpsites.net/web-design/add-widget-to-magazine-pro-front-page/ but it doesn't seem to work. Nothing shows up when I add a new widget into the new before-home-top widget.
I've tried replacing my front-page.php with
<?php
/**
* Magazine Pro.
*
* This file adds the front page to the Magazine Pro Theme.
*
* #package Magazine
* #author StudioPress
* #license GPL-2.0+
* #link http://my.studiopress.com/themes/magazine/
*/
add_action( 'genesis_meta', 'magazine_home_genesis_meta' );
/**
* Add widget support for homepage. If no widgets active, display the default loop.
*
* #since 3.0.0
*/
function magazine_home_genesis_meta() {
if ( is_active_sidebar( 'home-top' ) || is_active_sidebar( 'home-middle' ) || is_active_sidebar( 'home-bottom' ) ) {
// Force content-sidebar layout setting.
add_filter( 'genesis_site_layout', '__genesis_return_content_sidebar' );
// Add magazine-home body class.
add_filter( 'body_class', 'magazine_body_class' );
// Add homepage widgets.
add_action( 'genesis_before_content_sidebar_wrap', 'before_home_top' );
// Remove the default Genesis loop.
remove_action( 'genesis_loop', 'genesis_do_loop' );
// Add homepage widgets.
add_action( 'genesis_loop', 'magazine_homepage_widgets' );
}
}
// Add body class to front page.
function magazine_body_class( $classes ) {
$classes[] = 'magazine-home';
return $classes;
}
function before_home_top() {
genesis_widget_area( 'before-home-top', array(
'before' => '<div class="before-home-top widget-area">',
'after' => '</div>',
) );
}
// Output the widget areas for the front page.
function magazine_homepage_widgets() {
echo '<h2 class="screen-reader-text">' . __( 'Main Content', 'magazine-pro' ) . '</h2>';
genesis_widget_area( 'home-top', array(
'before' => '<div class="home-top widget-area">',
'after' => '</div>',
) );
genesis_widget_area( 'home-middle', array(
'before' => '<div class="home-middle widget-area">',
'after' => '</div>',
) );
genesis_widget_area( 'home-bottom', array(
'before' => '<div class="home-bottom widget-area">',
'after' => '</div>',
) );
}
genesis();
and I added the following to my functions.php
genesis_register_sidebar( array(
'id' => 'before-home-top',
'name' => __( 'Before Home Top', 'magazine-pro' ),
) );
I'm looking to add a custom HTML widget in there and nothing is showing up. Any help would be much appreciated!
The name of the sidebar you registered in functions.php is before-home-top.
There's a conditional in your front page code that is checking to see if any of three sidebars are active (contain widgets). The reason you're not seeing anything is because none of those checks are for before-home-top.
Your problem is this line:
if ( is_active_sidebar( 'home-top' ) || is_active_sidebar( 'home-middle' ) || is_active_sidebar( 'home-bottom' ) ) {
Add your sidebar to that list of checks:
if ( is_active_sidebar( 'before-home-top' ) || is_active_sidebar( 'home-top' ) || is_active_sidebar( 'home-middle' ) || is_active_sidebar( 'home-bottom' ) ) {
You may also wish to remove any sidebars that you don't intend to register.
So, I'm making my own theme from scratch for Wordpress with the Woocommerce plugin. I can seem to find how to add a sidebar widget to my Shop page... There is no area for it in thw Widgets menu and I can't seem to find how to create it and edit it...
Can someone help me understant how to create it and which files/code do I need to create in my child theme?
You need to create a widget area in your functions.php with this function :
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Sidebar',
'before_widget' => '<div class = "widget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
)
);
And for display in your theme :
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Sidebar") ) : ?>
Try this on header.php for example, you can add sidebar where u want
if ( is_active_sidebar( 'Sidebar' ) ) {
dynamic_sidebar( 'Sidebar' );
}
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