WordPress 动态小部件 - 如何删除标记和更改 class 名称?
WordPress dynamic widget - how to remove markups and change class names?
我添加了一个新的边栏
function child_register_sidebar(){
register_sidebar(array(
'name' => 'Social Media (Follow)',
'id' => 'sidebar-follow',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
}
在我的模板上,
<?php if ( is_active_sidebar( 'sidebar-follow' ) ) { ?>
<div class="follow-container">
<?php dynamic_sidebar( 'sidebar-follow' ); ?>
</div>
<?php } ?>
结果:
<div class="follow-container">
<div class="textwidget">
<a href="[full link to your Twitter]">
<img title="Twitter" alt="Twitter" src="https://socialmediawidgets.files.wordpress.com/2014/03/01_twitter1.png" width="35" height="35" />
</a>
</div>
</div>
如何删除 <div class="textwidget">
或将 class 名称更改为其他名称?
如果您想删除 class 或 'class' => 'myClass'
(如果您想使用自己的),请在注册边栏时将 'class' => ''
添加到您的参数中。
注册小部件看起来像这样:
function child_register_sidebar(){
register_sidebar(array(
'name' => 'Social Media (Follow)',
'id' => 'sidebar-follow',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
'class' => 'myClass'
));
}
尝试使用此代码删除 textwidget 并在 functions.php
中应用自己的 class
function register_my_widgets() {
register_sidebar(array(
'name' => 'test-widget',
'id' => 'test-widget',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
register_widget( 'My_Text_Widget' );
}
class My_Text_Widget extends WP_Widget_Text {
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
<?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>
<?php
echo $after_widget;
}
}
我添加了一个新的边栏
function child_register_sidebar(){
register_sidebar(array(
'name' => 'Social Media (Follow)',
'id' => 'sidebar-follow',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
}
在我的模板上,
<?php if ( is_active_sidebar( 'sidebar-follow' ) ) { ?>
<div class="follow-container">
<?php dynamic_sidebar( 'sidebar-follow' ); ?>
</div>
<?php } ?>
结果:
<div class="follow-container">
<div class="textwidget">
<a href="[full link to your Twitter]">
<img title="Twitter" alt="Twitter" src="https://socialmediawidgets.files.wordpress.com/2014/03/01_twitter1.png" width="35" height="35" />
</a>
</div>
</div>
如何删除 <div class="textwidget">
或将 class 名称更改为其他名称?
如果您想删除 class 或 'class' => 'myClass'
(如果您想使用自己的),请在注册边栏时将 'class' => ''
添加到您的参数中。
注册小部件看起来像这样:
function child_register_sidebar(){
register_sidebar(array(
'name' => 'Social Media (Follow)',
'id' => 'sidebar-follow',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
'class' => 'myClass'
));
}
尝试使用此代码删除 textwidget 并在 functions.php
中应用自己的 class function register_my_widgets() {
register_sidebar(array(
'name' => 'test-widget',
'id' => 'test-widget',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
register_widget( 'My_Text_Widget' );
}
class My_Text_Widget extends WP_Widget_Text {
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
<?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>
<?php
echo $after_widget;
}
}