如何为我的主题创建新的WordPress小部件?如何为我的主题创建新的wordpress小部件
0
A
回答
0
这是我关于最喜欢的教程:
http://justintadlock.com/archives/2009/05/26/the-complete-guide-to-creating-widgets-in-wordpress-28
我使用它作为一个基地,我所有的小部件。
基本上,小部件是一个插件,但是您扩展了类WP_Widget。这很容易,只需按照该教程。
而且这是有帮助的http://codex.wordpress.org/Widgets_API
祝你好运!
0
这是更多的问题谷歌比stackoverflow。你会在网上找到大量的教程和例子。在WordPress网站
1
如果你想创建一个小部件。导航到您的插件文件夹,并在“示例”文件夹中创建一个名为“sample.php”的空白文件。
将以下代码复制并粘贴到sample.php文件中。
<?php
/*
Plugin Name: Widget
Description: Widget Description
*/
// Creating the widget
// Create a class named as "sample_widget" that is a child class of "WP_Widget".
class sample_widget extends WP_Widget
{
//Constructor of class
public function __construct()
{
parent::__construct(
// Id of our widget.
'sample_widget',
// This is the widget name that will be visible to user
__('Sample Widget'),
// Description of our widget
array(
'description' => __('Description of our widget.')
));
}
// Creating widget front-end
// This is where the action happens
// Creating function named as "widget", receiving two parameters.
public function widget($args, $instance)
{
/*Getting and assigning our widget title to wordpress hook "widget_title"
and passing its value to "$title" */
$title = apply_filters('widget_title', $instance['title']);
// Area, that is before the widget is diplayed
echo $args['before_widget'];
// Checking "$title" is empty or not
if (!empty($title)) /* If "$title" is not empty, below code will execute.
$args['before_title'] -> Displaying content before our widget title
$title -> Display our widget title
$args['after_title'] -> Displaying content after our widget title */ {
echo $args['before_title'] . $title . $args['after_title'];
}
// Displaying text of our widget
echo __('Hello, this is our widget text!');
// Displaying content after our widget
echo $args['after_widget'];
}
// This function naming our widget title
public function form($instance)
{
// If title is already set.
if (isset($instance['title'])) {
// $title is getting already assigned title
$title = $instance['title'];
}
// Otherwise our default title will be "Widget title"
else {
$title = __('Widget title');
}
?>
<!-- These are the settings and user interface that an admin will see -->
<p>
<!-- Already set title will be displayed at the top of our widget in admin panel -->
<label for="<?php
echo $this->get_field_id('title');
?>"><?php
_e('Title:');
?></label>
<input class="widefat" id="<?php
echo $this->get_field_id('title');
?>" name="<?php
echo $this->get_field_name('title');
?>" type="text" value="<?php
echo esc_attr($title);
?>" />
</p>
<?php
}
// This function will replace old title with new title of our widget
public function update($new_instance, $old_instance)
{
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
return $instance;
}
}
// Function to register and load our newly widget
function sample_widget_load()
{
// Registering our widget named as "sample_widget"
register_widget('sample_widget');
}
// Calling our newly created function named as "sample_widget_load" to register our widget
add_action('widgets_init', 'sample_widget_load');
?>
相关问题
- 1. 如何在wordpress中创建一个主题友好的小部件?
- 2. Wordpress小部件创建
- 3. WordPress主题侧栏小部件问题
- 4. 创建WordPress主题:
- 5. WordPress的主题:强制创建新创建的缩略图大小
- 6. 如何为dojox.grid.cells.dijit创建一个新的小部件?
- 7. WordPress的小部件标题
- 8. 我如何自定义wordpress twentyten主题中的搜索小部件?
- 9. 为什么我的小部件更新配置活动创建?
- 10. 创建wordpress主题的问题
- 11. 如何创建自己的基本WordPress小部件
- 12. 为Wordpress Starkers主题创建菜单
- 13. 如何用我自己的行为重新创建一个TextField小部件?
- 14. 在Wordpress主题中创建文件夹
- 15. 如何在WordPress中创建新的自定义日历小部件?
- 16. wordpress小部件有什么用途?以及如何创建一个新的小部件?
- 17. 从html创建自己的wordpress主题
- 18. 何时创建Wordpress儿童主题?
- 19. Widgetizing主题,并创建自定义小部件插件
- 20. 如何为wordpress主题创建移动版本?
- 21. 如何在创建wordpress主题时包含css文件?
- 22. Wordpress新主题
- 23. 如何从我的Wordpress插件创建一个新页面
- 24. 如何在wordpress中创建我自己的新插件
- 25. 我应该建立一个新的主题或修改WordPress的现有主题?
- 26. 如何创建我的Web应用程序的小部件
- 27. Wordpress主题阻止显示的Javascript小部件
- 28. WordPress的 - 允许编辑管理主题小部件
- 29. 如何显示在我的wordpress主题
- 30. 如何在我的wordpress子主题中的所有页面上启用我的页脚小部件?