简体   繁体   English

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

[英]Widgetizing theme, and creating custom widget plugin

I have a few books I've been following on this. 我一直在关注这本书。 I made my own custom WP theme, which works fine, however I decided I wanted to make the right sidebar a widget area, and turn my twitter feed into a widget instead of hardcoding it into the template. 我制作了自己的自定义WP主题,效果很好,但是我决定要在右侧栏上创建一个小部件区域,然后将Twitter feed变成小部件,而不是将其硬编码到模板中。 I understand there are tons of twitter feed plugins out there, however I am doing this for the experience. 我知道那里有大量的Twitter提要插件,但是我这样做是为了体验。

Plugin file: 插件文件:

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

Registering sidebar as a widget-area: 将边栏注册为小部件区域:

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

Sidebar theme file: 侧边栏主题文件:

<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>

No matter what I do, it always displays "sp_rightSidebar is waiting on the sidebar", I have tested my widget plugin with other themes and it works fine, so it has to be something about my sidebar theme file/not registering the sidebar correctly I'm assuming. 不管我做什么,它始终显示“ sp_rightSidebar正在等待侧栏”,我已经用其他主题测试了我的小部件插件,并且工作正常,因此必须与我的侧栏主题文件有关/不能正确注册侧栏我假设。 The "Right Sidebar" widget area does display in the widgets area in the admin panel, however anything added does not stay there. “右侧边栏”窗口小部件区域确实显示在管理面板的窗口小部件区域中,但是添加的任何内容都不会保留在那里。

I hate to be the guy who dumps his code asking for people to take a look, but if you see anything that could be wrong, I'd appreciate your input. 我不愿意成为遗弃代码的人,要求人们看看,但是如果您发现任何可能出错的地方,我将不胜感激。

Thanks! 谢谢!

sp_rightSidebar function doesn't return anything, so !sp_rightSidebar() will always be true. sp_rightSidebar函数不返回任何内容,因此!sp_rightSidebar()始终为true。 I don't understand what you're trying to check with that conditional. 我不明白您要使用该条件检查什么。 Perhaps you want to check if the sidebar is active with is_active_sidebar ? 也许您想使用is_active_sidebar检查侧边栏是否处于活动状态?

I don't understand why you are calling register_sidebar outside of your init action. 我不明白为什么您要在init操作之外调用register_sidebar


Your sidebar ID must be all lowercase, so 'rightcolumn'. 您的侧边栏ID必须全部为小写,因此为“ rightcolumn”。 See the codex: http://codex.wordpress.org/Function_Reference/register_sidebar#Parameters 参见编解码器: http : //codex.wordpress.org/Function_Reference/register_sidebar#Parameters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM