小部件化主题,并创建自定义小部件插件
我一直在关注这方面的几本书。我制作了自己的自定义 WP 主题,效果很好,但是我决定将右侧边栏设为小部件区域,并将我的 twitter feed 转换为小部件,而不是将其硬编码到模板中。我知道那里有大量的 Twitter feed 插件,但我这样做是为了体验。
插件文件:
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 is waiting on the sidebar”,我已经用其他主题测试了我的小部件插件,它工作正常,所以它有关于我的侧边栏主题文件/我假设没有正确注册侧边栏。 “右侧栏”小部件区域确实显示在管理面板的小部件区域中,但是添加的任何内容都不会保留在那里。
我讨厌成为那个抛弃自己的代码并要求人们查看的人,但如果您发现任何可能错误的地方,我将不胜感激您的意见。
谢谢!
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. I understand there are tons of twitter feed plugins out there, however I am doing this for the experience.
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. 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
函数不会返回任何内容,因此!sp_rightSidebar()
将始终为 true。我不明白你想用这个条件检查什么。也许您想使用is_active_sidebar
检查侧边栏是否处于活动状态?我不明白您为什么在
init
操作之外调用register_sidebar
。您的侧边栏 ID 必须全部小写,即“rightcolumn”。请参阅代码:http://codex.wordpress.org/Function_Reference/register_sidebar#Parameters
sp_rightSidebar
function doesn't return anything, so!sp_rightSidebar()
will always be true. I don't understand what you're trying to check with that conditional. Perhaps you want to check if the sidebar is active withis_active_sidebar
?I don't understand why you are calling
register_sidebar
outside of yourinit
action.Your sidebar ID must be all lowercase, so 'rightcolumn'. See the codex: http://codex.wordpress.org/Function_Reference/register_sidebar#Parameters