Php 如何使用函数将文本小部件添加到wordpress边栏?
我想在激活主题时将文本小部件添加到我的站点侧边栏中。如何使用wordpress函数或其他方法来实现这一点?我正在使用WordPress4.1。Php 如何使用函数将文本小部件添加到wordpress边栏?,php,wordpress,Php,Wordpress,我想在激活主题时将文本小部件添加到我的站点侧边栏中。如何使用wordpress函数或其他方法来实现这一点?我正在使用WordPress4.1。 帮助我。谢谢。我不知道你想要创建什么样的小部件,所以我给你一个简单的例子,让你能理解 在functions.php中编写blow代码,或者创建一个外部文件并将其包含到functions.php中 首先注册小部件: //========= Your Custom Widget add_action( 'widgets_init', 'your_simple
帮助我。谢谢。我不知道你想要创建什么样的小部件,所以我给你一个简单的例子,让你能理解 在functions.php中编写blow代码,或者创建一个外部文件并将其包含到functions.php中 首先注册小部件:
//========= Your Custom Widget
add_action( 'widgets_init', 'your_simple_widget' );
function your_simple_widget() {
register_widget( 'Your_Simple_Same_Widget' );
}
其次,创建一个小部件主体:
class Your_Simple_Same_Widget extends WP_Widget { }
注意:注册小部件(“你的简单相同的小部件”)代码>名称和分类您的\u简单\u相同\u小部件
和小部件设置名称必须相同
课堂上有四个小部件
小部件设置
如何在前端显示
更新小部件值
管理面板中的小部件窗体或字段
好的,第三个班写以下代码:
//========= Your Custom Widget Body
class Your_Simple_Same_Widget extends WP_Widget {
//=======> Widget setup
function Your_Simple_Same_Widget() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'your class', 'description' => __('Your Class Description widget', 'your class') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'your_widget' );
/* Create the widget. */
$this->WP_Widget( 'your_widget', __('Widget Heading', 'your class'), $widget_ops, $control_ops );
}
//=======> How to display the widget on the screen.
function widget($args, $widgetData) {
extract($args);
//=======> Our variables from the widget settings.
$your_widget_title = $widgetData['txtYourTitle'];
$your_widget_description = $widgetData['txtYourDescription'];
//=======> widget body
echo $before_widget;
echo '<div class="">';
if($your_widget_title){
echo '<h2>'.$your_widget_title .'</h2>';
}
if($your_widget_description){
echo '<p>'.$your_widget_description .'</p>';
}
echo "</div>";
echo $after_widget;
}
//=======>Update the widget
function update($new_widgetData, $old_widgetData) {
$widgetData = $old_widgetData;
//Strip tags from title and name to remove HTML
$widgetData['txtYourTitle'] = $new_widgetData['txtYourTitle'];
$widgetData['txtYourDescription'] = $new_widgetData['txtYourDescription'];
return $widgetData;
}
//=======>widget Display
function form($widgetData) {
//Set up some default widget settings.
$widgetData = wp_parse_args((array) $widgetData);
?>
<p>
<label for="<?php echo $this->get_field_id('txtYourTitle'); ?>">Widget Title:</label>
<input id="<?php echo $this->get_field_id('txtYourTitle'); ?>" name="<?php echo $this->get_field_name('txtYourTitle'); ?>" value="<?php echo $widgetData['txtYourTitle']; ?>" style="width:275px;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('txtYourDescription'); ?>">Widget Title:</label>
<input id="<?php echo $this->get_field_id('txtYourDescription'); ?>" name="<?php echo $this->get_field_name('txtYourDescription'); ?>" value="<?php echo $widgetData['txtYourDescription']; ?>" style="width:275px;" />
</p>
<?php
}
}
?>
/==========您的自定义小部件主体
将您的\u简单\u相同的\u小部件扩展到WP\u小部件{
//=======>小部件设置
函数您的\u Simple\u Same\u小部件(){
/*小部件设置*/
$widget_ops=array('classname'=>'your class','description'=>uu('your class description widget','your class');
/*控件设置*/
$control_ops=array('width'=>300,'height'=>350,'id_base'=>your_widget');
/*创建小部件*/
$this->WP_小部件('your_小部件','Widget Heading','your class'),$Widget_ops,$control_ops);
}
//=======>如何在屏幕上显示小部件。
函数小部件($args,$widgetData){
摘录($args);
//=======>小部件设置中的变量。
$your_widget_title=$widgetData['txtYourTitle'];
$your_widget_description=$widgetData['txtYourDescription'];
//=======>小部件主体
echo$before_小部件;
回声';
如果($your_widget_title){
回显“.$your_widget_title.”;
}
如果($your_widget_description){
回显“”。$your_widget_description.“”;
}
回声“;
echo$after_小部件;
}
//=======>更新小部件
函数更新($new\u widgetData,$old\u widgetData){
$widgetData=$old_widgetData;
//从标题和名称中删除标记以删除HTML
$widgetData['txtYourTitle']=$new_widgetData['txtYourTitle'];
$widgetData['txtYourDescription']=$new_widgetData['txtYourDescription'];
返回$widgetData;
}
//=======>小部件显示
函数形式($widgetData){
//设置一些默认小部件设置。
$widgetData=wp_parse_args((数组)$widgetData);
?>