admin管理员组文章数量:1429064
In the Appearance -> Widgets menu there is a list of widgets that you can drag and drop to show in the sidebar.
Where is the HTML/PHP code for these custom widgets?
I've been on WordPress's Function Reference but couldn't find anything. Surely these widgets must be pulled from a HTML/PHP template.
The reason I want to know is the widget titles by default are <h3>
tags and i want to change them to <h5>
tags. Also i need to add some <hr />
's and other things.
I've looked in the theme/includes/widgets.php
file but found nothing.
I'm using a copy of Twenty Eleven to modify my theme by the way.
The code in theme/sidebar.php
is for (!dynamic_sidebar())
, however my sidebar is dynamic so this code is useless.
In the Appearance -> Widgets menu there is a list of widgets that you can drag and drop to show in the sidebar.
Where is the HTML/PHP code for these custom widgets?
I've been on WordPress's Function Reference but couldn't find anything. Surely these widgets must be pulled from a HTML/PHP template.
The reason I want to know is the widget titles by default are <h3>
tags and i want to change them to <h5>
tags. Also i need to add some <hr />
's and other things.
I've looked in the theme/includes/widgets.php
file but found nothing.
I'm using a copy of Twenty Eleven to modify my theme by the way.
The code in theme/sidebar.php
is for (!dynamic_sidebar())
, however my sidebar is dynamic so this code is useless.
2 Answers
Reset to default 10The WordPress Widgets API is how various widgets are created and sidebars registered.
When creating a new widget there are variables that can be added to any widget. Those get their value from the register_sidebars
arguments.
args (string/array) (optional)
Builds Sidebar based off of 'name' and 'id' values. Default: None
name
- Sidebar name.
id
- Sidebar id.
before_widget
- HTML to place before every widget.
after_widget
- HTML to place after every widget.
before_title
- HTML to place before every title.
after_title
- HTML to place after every title.
Example:
<?php
add_action( 'widgets_init', 'prefix_register_sidebars' );
function prefix_register_sidebars() {
$args = array(
'name' => 'My Sidebar',
'id' => 'my-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',,
'after_widget' => '</div><hr />',
'before_title' => '<h5 class="widgettitle">',
'after_title' => '</h5>'
);
register_sidebars( $args );
}
Example Widget:
class MY_Widget extends WP_Widget {
function my_widget( $args, $instance ) {
$widget_ops = array(
'description' => 'My Widget Description'
);
parent::WP_Widget(false, 'My Widget Name', $widget_ops );
}
function widget() { // This controls the display of the widget
$title = 'My Widget Title';
echo $before_widget; // Outputs the the 'before_widget' register_sidebars setting
echo $title; //Will be wrapped in the 'before_title' and 'after_title' settings
echo '<p>This is my widget output</p>';
echo $after_widget; //Outputs the 'after_widget' settings
}
}
add_action( 'widgets_init', 'prefix_register_widgets' );
function prefix_register_widgets() {
register_widget( 'my_widget' );
}
it is in the functions.php
function twentyeleven_widgets_init() {
register_widget( 'Twenty_Eleven_Ephemera_Widget' );
register_sidebar( array(
'name' => __( 'Main Sidebar', 'twentyeleven' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
本文标签: How to edit widgets in WordPress
版权声明:本文标题:How to edit widgets in WordPress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745543676a2662606.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
dynamic_sidebar()
will do one of two things when called, it will either echo the sidebar, or return false. Soif( !dynamic_sidebar() )
will either echo the sidebar, or do some sort of fallback. – mor7ifer Commented Jan 16, 2012 at 13:46