以编程方式在wordpress中显示HTML小部件

时间:2014-02-02 12:44:54

标签: wordpress widget

我在WordPress中创建了三个HMTL小部件,我为它们分配了页脚区域。现在我想使用此代码<?php the_widget( 'WP_Widget_Text', 'title=Latest Tours' ); ?>在页脚中显示它们。Latest Tour是小部件标题。但它只显示标题没有内部数据。我错过了什么?

我的页脚代码是这样的:

<div class="col_1">
                    <h2>Other Links</h2>
                    <?php the_widget( 'WP_Widget_Text', 'title=Latest Tours' ); ?> 
</div>

function.php代码:

function presscore_widgets_init() {

        if ( function_exists('of_get_option') ) {

            $w_params = array(
                'before_widget' => '<section id="%1$s" class="widget %2$s">',
                'after_widget'  => '</section>',
                'before_title'  => '<div class="widget-title">',
                'after_title'   => '</div>'
            );

            $w_areas = apply_filters( 'presscore_widgets_init-sidebars', of_get_option( 'widgetareas', false ) );

            if ( !empty( $w_areas ) && is_array( $w_areas ) ) {

                $prefix = 'sidebar_';

                foreach( $w_areas as $sidebar_id=>$sidebar ) {

                    $sidebar_args = array(
                        'name'          => isset( $sidebar['sidebar_name'] ) ? $sidebar['sidebar_name'] : '',
                        'id'            => $prefix . $sidebar_id,
                        'description'   => isset( $sidebar['sidebar_desc'] ) ? $sidebar['sidebar_desc'] : '',
                        'before_widget' => $w_params['before_widget'],
                        'after_widget'  => $w_params['after_widget'],
                        'before_title'  => $w_params['before_title'],
                        'after_title'   => $w_params['after_title'] 
                    );

                    $sidebar_args = apply_filters( 'presscore_widgets_init-sidebar_args', $sidebar_args, $sidebar_id, $sidebar );

                    register_sidebar( $sidebar_args );
                }
            }
        }
    }

endif; // presscore_widgets_init

add_action( 'widgets_init', 'presscore_widgets_init' );

OUPUT:

<pre>Array
(
    [1] =&gt; Array
        (
            [sidebar_name] =&gt; Default Sidebar
            [sidebar_desc] =&gt; Sidebar primary widget area
        )

    [2] =&gt; Array
        (
            [sidebar_name] =&gt; Default Footer
            [sidebar_desc] =&gt; Footer primary widget area
        )

)
</pre>

1 个答案:

答案 0 :(得分:1)

将以下代码添加到footer.php文件中:

<?php dynamic_sidebar( 'sidebar_2' ); ?>