【问题标题】:Wordpress - How to show custom widgetWordpress - 如何显示自定义小部件
【发布时间】:2011-12-25 17:32:21
【问题描述】:

我是 wordpress 的新手。我想创建自己的自定义小部件并在侧菜单上显示内容。但是,即使它显示在 wordpress 的控制面板中,wordpress 也不会显示小部件。我将 CustomWidget 添加到 Primary Widget Area。

编辑:我注意到“小部件”函数没有被调用,因为它应该是进行渲染的那个。

class CustomWidget extends WP_Widget{
    function CustomWidget(){
        $widget_ops = array('classname' => 'custom_widget', 'description' => __( "Custom Widget") );
        $control_ops = array('width' => 250, 'height' => 400, 'id_base' => 'custom-widget');
        $this->WP_Widget('CustomWidget', 'Custom Widget', $widget_ops, $control_ops);
    }

    function widget($args, $instance){
        extract($args);
        $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
        $menu_order = $instance['menu_order'];
        $show_siblings = $instance['show_siblings'];
        $exclude = $instance['exclude'];
        echo $before_widget;
        echo $before_title;
        echo 'Title!!!';
        echo $after_title;
        echo 'Content!!!';
        echo $after_widget;
    }
}

function CustomWidget_init(){
    register_widget('CustomWidget');
}
add_action("widgets_init", "CustomWidget_init");

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    在你的代码末尾添加这个

    add_action('TB_RenderWidget', array('CustomWidget', 'render'),10,12 );
    

    【讨论】:

    • 不幸的是没有工作。 CustomWidget 构造函数被命中,只是由于某种原因而不是渲染函数。
    • 我忘记了什么。你需要包含一个像这样的函数 code function render($before_widget,$after_widget,$title){ echo $before_widget; if ( !empty( $title ) ) { echo $before_title . $标题。 $after_title; }; //这里你调用任何你使用的东西 tu print content echo $after_widget; }code
    猜你喜欢
    • 2019-08-19
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多