Wordpress:在不使用文本小部件的情况下将html代码添加到小部件区域

[英]Wordpress: Add html code to widget area without using a text widget


Normally, you can create a text widget if you want to add html code to the sidebar area of Wordpress. But this won't work in my case, let me explain it to you:

通常,如果要将HTML代码添加到Wordpress的侧边栏区域,可以创建文本小部件。但这不适用于我的情况,让我向你解释一下:

I want to place an image (with an link) to the sidebar, which doesn't end up in a widget box with white background. Instead the image should be placed directly in the widget area.

我想将图像(带有链接)放置到侧边栏,侧边栏不会在带有白色背景的小部件框中结束。相反,图像应直接放在小部件区域中。

Currently, my wordpress theme applies a white background to all widgets with the css class .widget by default. As far as I know, every widget is assigned with this class.

目前,我的wordpress主题默认情况下使用css类.widget将白色背景应用于所有小部件。据我所知,每个小部件都分配有这个类。

Is there a way to inject the html code using a hook in the functions.php of my theme folder? Or maybe using the sidebar.php file?

有没有办法在我的主题文件夹的functions.php中使用钩子注入html代码?或者也许使用sidebar.php文件?

2 个解决方案

#1


1  

You can use Widgets API

您可以使用Widgets API

if ( !class_exists( 'MyCustomWidgetClass' ) ) {
        // Create Custom Draggable Widget  
    class MyCustomWidgetClass extends WP_Widget {
        // Register widget with WordPress.
        public function __construct() {

            parent::__construct(
                'my_custom_widget', 
                'My Custom Widget Title', 
                array( 'description' => __( 'Custom Widget Decription', 'cmk' ), ) 
            );
        }
        //Front-end display of widget.
        public function widget( $args, $instance ) {
            $content = 'Bla Bla'; // Add HTML code, or call the function that contains your HTML code
            extract( $args );
            $title = apply_filters( 'widget_title', $instance['title'] );
            echo $before_widget;
            if ( ! empty( $title ) )
            echo $before_title . $title . $after_title;
            echo $content;
            echo $after_widget;
        }

        //Sanitize widget form values as they are saved.
        public function update( $new_instance, $old_instance ) {
            $instance = array();
            $instance['title'] = strip_tags( $new_instance['title'] );
            return $instance;
        }

        // Back-end widget form.
        public function form( $instance ) {
            if ( isset( $instance[ 'title' ] ) ) {
                $title = $instance[ 'title' ];
            } else {
                $title = __( 'My Custom Widget Title', 'cmk' );
            } ?>
            <p>
            <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 
        }

    } 
    add_action( 'widgets_init', create_function('', 'return register_widget("MyCustomWidgetClass");') );
}

#2


0  

You can use the inline css to change the background color of your image if you want to add image using text widget.

如果要使用文本小部件添加图像,可以使用内联css更改图像的背景颜色。

<a href="http://www.w3schools.com/">
<img src="Your Image Path" style="background-color: beige;">
</a>

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/11/26/afd074c74d22f2bc343550a864b3a023.html



 
  © 2014-2022 ITdaan.com 联系我们: