小部件内容未显示在wordpress的页面上

时间:2019-01-24 10:59:58

标签: php wordpress

我有一个标准小部件,该小部件已包含在

的页脚中
<?php get_sidebar(); ?>

窗口小部件的内容仅在类别页面中可见。例如archive.php

我如何也可以在其他页面上显示它?它包含在所有页面的页脚中,如下所示:

<?php get_footer(); ?>

在footer.php中,我有

<div class="footer-center" data-equalizer-watch>
   <div class="baseline">
       <div class="newsletter">
            <?php get_sidebar(); ?>
       </div>
   </div>
</div>

更新

<div class="newsletter">
     <?php dynamic_sidebar( 'sidebar' ); ?>
</div>

只需将get_sidebar();更改为dynamic_sidebar( 'sidebar' );

functions.php中的窗口小部件功能

//Stay Connected widget
add_filter( 'widget_text', 'do_shortcode' );

function stay_connected_load_widget() {
    register_widget( 'stay_connected_widget' );
}
add_action( 'widgets_init', 'stay_connected_load_widget' );

class stay_connected_widget extends WP_Widget { 
    function __construct() {
        parent::__construct( 
            'stay_connected_widget',  
            __('Stay Connected', 'stay_connected_widget_domain'), 
            array( 'description' => __( 'Social networks and subscribe form', 'stay_connected_widget_domain' ))
        );
    }

    //Widget frontend
    public function widget( $args, $instance ) {
        $cf7_form = apply_filters( 'widget_title', $instance['cf7_form'] );
        echo $args['before_widget'];
    //  echo $args['before_title'] . '<h4>' .  __( 'Stay Connected', 'stay_connected_widget_domain' ) . '</h4>' .$args['after_title'];
        ?>

        <?php           
        if(!empty( $cf7_form )){
            echo '<h4 class="form_title">'. __('Subscribe By Email').'</h4>';
            echo '<div class="stay_connected_form">'.do_shortcode(html_entity_decode($cf7_form)).'</div>';
        }       
        echo $args['after_widget'];
    }

    // Widget Backend 
    public function form( $instance ) {
        if ( isset( $instance[ 'cf7_form' ] ) ) {
            $cf7_form = $instance[ 'cf7_form' ];
        }
        else {
            $cf7_form = __( '', 'stay_connected_widget_domain' );
        }
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'cf7_form' ); ?>"><?php _e( 'Contact Form:' ); ?></label> 
            <input class="widefat" id="<?php echo $this->get_field_id( 'cf7_form' ); ?>" name="<?php echo $this->get_field_name( 'cf7_form' ); ?>" type="text" value="<?php echo esc_attr( $cf7_form ); ?>" placeholder="[<?php _e('contact form shortcode'); ?>]"/>
        </p>
        <?php 
    }
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['cf7_form'] = ( ! empty( $new_instance['cf7_form'] ) ) ? strip_tags( $new_instance['cf7_form'] ) : '';
        return $instance;
    }
}

更新

Array ( 
    [wp_inactive_widgets] => Array ( ) 
    [header] => Array ( 
        [0] => media_gallery-2 
    ) 
    [footer] => Array ( 
        [0] => custom_html-2 
    ) 
    [sidebar] => Array ( 
        [0] => stay_connected_widget-3 
        [1] => search-2 
    ) 
    [array_version] => 3 
)

1 个答案:

答案 0 :(得分:0)

将此代码添加到functions.php中

function stay_widgets_init() {

    register_sidebar( array(
        'name'          => 'Stay Connected ',
        'id'            => 'stay_connected_widget',
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '',
        'after_title'   => '',
    ) );

}
add_action( 'widgets_init', 'stay_widgets_init' );

在footer.php文件中使用此

<div class="newsletter">
     <?php dynamic_sidebar( 'stay_connected_widget' ); ?>
</div>