我最近刚刚在Wordpress主题中创建了一个小部件,将小部件插入到侧边栏中。
它基于文本小部件。然而,html内容是硬编码到小部件中的,并且它有自己的标题,这样,如果用户决定将小部件从侧边栏中删除,那么如果他们改变主意并希望将其恢复到该侧边栏或另一个侧边栏上,小部件将始终可以在“可用小部件”部分中重用。
代码如下:
<?php
/* Plugin Name: Basic Text Widget
Plugin URI: www.lancscps.co.uk
Description: Text to put into the sidebar regarding free consultation
Version: 1.0
Author: Jonathan Beech
Author URI: www.lancscps.co.uk
*/
class freeconsultation extends WP_Widget {
function freeconsultation() {
$widget_ops = array(
\'classname\' => \'freeconsultation\',
\'description\' => \'Text to put into the sidebar regarding free consultation\'
);
$this->WP_Widget(
\'freeconsultation\',
\'Free Consultation\',
$widget_ops
);
}
function widget($args, $instance) { // widget sidebar output
extract($args, EXTR_SKIP);
echo $before_widget; // pre-widget code from theme
print <<<EOM
<div class="consultation">
<p>In order to establish how we can proceed together with your needs, our first meeting is free of charge.</p>
</div><!--end sub consultation-->
EOM;
echo $after_widget; // post-widget code from theme
}
}
add_action(
\'widgets_init\',
create_function(\'\',\'return register_widget("freeconsultation");\')
);
我希望有一种方法可以将自定义贴子插入到小部件中,取代已经存在的HTML。也许这可以通过id来完成,这样用户就可以调整帖子的内容,从而调整他们认为合适的小部件。对最终用户来说,更改代码文件是不可行的。