2011-05-21 94 views
0

我有几本书,我一直在关注这个。我做了我自己定制的WP主题,这很好,但是我决定我想让右侧边栏成为一个小部件区域,并将我的Twitter feed变成一个小部件,而不是将其硬编码到模板中。我知道那里有大量的twitter feed插件,但是我正在为此体验做这件事。Widgetizing主题,并创建自定义小部件插件

插件文件:

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");')); 

注册侧边栏以小工具领域:

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'); 

边栏的主题文件:

<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_rightSidebar在侧边栏上等待“,我已经测试了与其他主题的小部件插件,它工作正常,所以它必须是关于我的侧边栏主题文件/没有正确注册侧边栏我假设。 “右侧栏”小部件区域显示在管理面板的小部件区域中,但添加的任何内容都不会保留在那里。

我讨厌成为那些倾倒他的代码,要求人们去看看的人,但是如果你发现任何可能出错的东西,我会很感激你的意见。

谢谢!

回答

1

sp_rightSidebar函数不返回任何内容,因此!sp_rightSidebar()将始终为真。我不明白你想用那个条件来检查你的。也许你想检查侧栏是否与is_active_sidebar一起生效?

我不明白你为什么要在init行动之外致电register_sidebar


你的侧边栏的ID必须全部小写,所以 'rightcolumn'。看到codex:http://codex.wordpress.org/Function_Reference/register_sidebar#Parameters

+0

这是有道理的,所以我改变它来检查它是否是积极的,它永远不会返回true。我修改了在函数内调用的register_sidebar动作。 正如我之前所说的,我没有把任何东西放在小工具菜单中的边栏中,因此我仍然无能为力。 : -/ – user652650 2011-05-21 17:59:02

+0

@ user652650您的侧边栏ID实际上必须全部小写,因此'rightcolumn'。 – 2011-05-21 18:31:40