admin管理员组

文章数量:1122832

I want to display a widget outside the sidebar, but I can't get my code to work.

The theme name is radius-primary-sidebar and my widget id is text-20 (according to the html in the admin page. See image #1).

I've tried using the_widget("radius-primary-sidebar", "widget-id=text-20"), but that didn't return anything.

What I'm I doing wrong?

I'm using wordpress 3.5.2.

From /wp-admin/widgets.php in the admin panel.

I want to display a widget outside the sidebar, but I can't get my code to work.

The theme name is radius-primary-sidebar and my widget id is text-20 (according to the html in the admin page. See image #1).

I've tried using the_widget("radius-primary-sidebar", "widget-id=text-20"), but that didn't return anything.

What I'm I doing wrong?

I'm using wordpress 3.5.2.

From /wp-admin/widgets.php in the admin panel.

Share Improve this question asked Apr 16, 2014 at 22:50 Linus OleanderLinus Oleander 1191 silver badge2 bronze badges 3
  • Where exactly outside the sidebar? You're referring to a native widget where the i.d changes each time you use a different text widget. I assume you want to create a new widget area? – Brad Dalton Commented Apr 17, 2014 at 11:50
  • I want to display a single widget in the bottom of my mobile site. – Linus Oleander Commented Apr 17, 2014 at 11:56
  • How are you generating your mobile site? – Brad Dalton Commented Apr 17, 2014 at 12:06
Add a comment  | 

3 Answers 3

Reset to default 0

According to the codex, the first argument should be a valid widget class name and not the theme name.

<?php the_widget( 'Replace_With_Valid_Widget_Class_Name', 'widget-id=text-20' ); ?>

This is how you add a widget from your functions file.

Note: Only displays on mobiles.

//functions.php
function wpsites_register_widget() {

 register_sidebar( array(
'name' => 'Bottom Widget',
'id' => 'bottom-widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
) );
}

add_action( 'widgets_init', 'wpsites_register_widget' );

add_filter( 'loop_end', 'bottom_widget', 25 );

function bottom_widget() {

if ( wp_is_mobile() && is_active_sidebar( 'bottom-widget' ) ) { 
dynamic_sidebar('bottom-widget', array(
'before' => '<div class="bottom-widget">',
'after' => '</div>',
) );
     }

}

You can change the loop_start hook to loop_end or use any other WordPress or theme specific hook.

You can also add the widget in a template file like single.php

<?php if ( wp_is_mobile() && is_active_sidebar( 'bottom-widget' ) ) : ?>
<ul id="bottom-widget">
<?php dynamic_sidebar( 'bottom-widget' ); ?>
</ul>
<?php endif; ?>

Uses wp_is_mobile() to check if browser being used on a mobile device.

You should use CSS Media Queries to style your widget for display on different sized mobile devices.

Please Try with bellow inside functions.php

function wpsites_register_widget() {
 register_sidebar( array(
'name' => 'Bottom Widget',
'id' => 'bottom-widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
) );

Copy the code and pastest as you want to show the widgets

if (is_active_sidebar( 'bottom-widget' ) ) :
dynamic_sidebar( 'bottom-widget' ); 
endif;

本文标签: Display widget outside sidebar