Php Widgetizing主题,并创建自定义小部件插件

Php Widgetizing主题,并创建自定义小部件插件,php,widget,wordpress,wordpress-theming,Php,Widget,Wordpress,Wordpress Theming,我有几本关于这方面的书。我制作了自己的自定义WP主题,效果很好,但我决定将右侧边栏作为小部件区域,并将我的twitter提要变成小部件,而不是将其硬编码到模板中。我知道有很多推特提要插件,不过我这么做是为了体验 插件文件: class sp_twitterWidget extends WP_Widget { function sp_twitterWidget() { parent::WP_Widget(false, $name = 'Custom Twitter

我有几本关于这方面的书。我制作了自己的自定义WP主题,效果很好,但我决定将右侧边栏作为小部件区域,并将我的twitter提要变成小部件,而不是将其硬编码到模板中。我知道有很多推特提要插件,不过我这么做是为了体验

插件文件:

class sp_twitterWidget extends WP_Widget 
{
    function sp_twitterWidget()
    {
        parent::WP_Widget(false, $name = 'Custom Twitter Feed');
        return;
    }

    function widget($args, $instance)
    {
        extract($args);

        echo $before_widget;
            echo $before_title;
                echo $instance['title'];
            echo $after_title;

                $api_url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=';
                $twitter_data = file_get_contents($api_url);
                $twitter_data = json_decode($twitter_data);

                for ($i=1;$i<=3;$i++):
                    echo '<p class="tweet">';
                    echo $twitter_data[$i]->text;
                    echo '<br /><span>';
                    echo date("F j", strtotime($twitter_data[$i]->created_at));
                    echo '</span></p>';
                endfor;

        echo $after_widget;

    }

    function update($new_instance, $old_instance)
    {
        return $new_instance;
    }

    function form($instance)
    {
        $theTitle = esc_attr($instance['title']);

        echo '<p>';
            echo '<label for="'.$this->get_field_id('title').'">
                  Title: <input class="widefat" id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$theTitle.'" />
                  </label>';
        echo '</p>';
    }    
}

add_action('widgets_init', create_function('', 'return register_widget("sp_twitterWidget");')); 
class sp\u twitterWidget扩展了WP\u Widget
{
函数sp_twitterWidget()
{
父::WP_小部件(false,$name='customtwitterfeed');
返回;
}
函数小部件($args$instance)
{
摘录($args);
echo$before_小部件;
echo$在标题之前;
echo$instance['title'];
echo$在标题之后;
$api\u url='1http://api.twitter.com/1/statuses/user_timeline.json?screen_name=';
$twitter\u data=file\u get\u contents($api\u url);
$twitter\u data=json\u decode($twitter\u data);
对于($i=1;$itext;
回声“
”; 回音日期(“fj”,strotime($twitter_data[$i]->created_at)); 回声“

”; endfor; echo$after_小部件; } 函数更新($new\u实例,$old\u实例) { 返回$new_实例; } 函数形式($instance) { $theTitle=esc_attr($instance['title']); 回声“”; 回声' 标题: '; 回声“

”; } } 添加操作('widgets_init',创建函数(''returnregister_widget('sp_twitterWidget')));
将侧栏注册为小部件区域:

if (!function_exists('register_sidebar')) { register_sidebar(); }

function sp_rightSidebar()
{
    register_sidebar(array(
        'name'          => 'Right Sidebar',
        'id'            => 'rightColumn',
        'description'   => __('The widget area for the right sidebar', 'simplePortfolio'),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<div class="rightHeading">',
        'after_title'   => '</div>'));    
}

add_action('init', 'sp_rightSidebar'); 
如果(!function_存在('register_sidebar'){register_sidebar();}
函数sp_rightSidebar()
{
寄存器侧栏(数组)(
'名称'=>'右侧边栏',
'id'=>'rightColumn',
'description'=>uuuu('右侧边栏的窗口小部件区域','simplePortfolio'),
'在小部件'=>''之前,
'在小部件'=>''之后,
“在标题“=>”之前,
“在标题之后”=>”);
}
添加动作('init','sp_rightSidebar');
侧边栏主题文件:

<div id="rightColumn"> 
        <?php if (!function_exists('sp_rightSidebar') || !sp_rightSidebar()): ?>
        sp_rightSidebar is waiting.
        <?php else: ?>
        <?php dynamic_sidebar('sp_rightSidebar'); ?>
        <?php endif; ?>    
</div>

sp_右侧边栏正在等待。
无论我做什么,它总是显示“sp_rightSidebar正在侧边栏上等待”,我已经用其他主题测试了我的小部件插件,它工作正常,所以它必须是关于我的侧边栏主题文件/我假设它没有正确注册侧边栏。“右侧侧边栏”小部件区域确实显示在管理面板的小部件区域中,但是添加的任何内容都不会停留在那里

我不喜欢把自己的代码丢给别人看,但如果你发现有什么地方可能出错,我会很感激你的意见


谢谢!

sp_rightSidebar
函数不返回任何内容,因此
!sp_rightSidebar()
将始终为真。我不明白您试图用该条件检查什么。也许您想用该条件检查侧栏是否处于活动状态

我不明白你为什么在
init
操作之外调用
register\u侧边栏



您的边栏ID必须全部为小写,因此为“rightcolumn”。请参阅代码:

,这很有意义,因此我将其更改为检查它是否处于活动状态,并且它永远不会返回true。我修改了register_边栏操作,以便在函数中调用。正如我前面所说,我在widgets菜单的边栏中没有任何内容保留,因此我仍然保留clueless.:-/@user652650您的边栏ID实际上需要全部小写,因此“rightcolumn”。