我正在尝试为一个子主题扩展wordpress (v5.1.1)中的默认图库小部件。小部件显示在仪表板的“小部件”部分,但表单字段从不显示以编辑小部件设置。下面显示了默认图库小部件的行为,然后是我的小部件(称为“扩展图库”)。
我做了什么:我将/wp-includes/widgets/class-wp-widget-media-gallery.php的内容复制并粘贴到我的子主题中的一个名为extended-gallery.php的文件中。这两个文件是完全相同的,除了在extended-gallery.php的开头,我更改了类名和句柄。
see class-wp-widget-media-gallery.php
我在extended-Gallery.php中所做的更改:
class Extended_Gallery extends WP_Widget_Media {
/**
* Constructor.
*
* @since 4.9.0
*/
public function __construct() {
parent::__construct(
'media_gallery_extended',
__( 'Extended Gallery' ),
array(
'description' => __( 'Displays an image gallery.' ),
'mime_type' => 'image',
)
);
在functions.php中,我注册了extended-gallery.php
<?php
//custom widgets
require_once("extended-gallery.php");
add_action("widgets_init", "custom_widgets_init");
function custom_widgets_init(){
register_widget("Extended_Gallery");
}
// add custom style
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version')
);
}
?>
如何让自定义小部件的设置字段与默认的图库小部件以相同的方式工作?如果我添加额外的字段,它会搞砸吗?
发布于 2019-03-29 08:46:41
你的问题是关于在wordpress上创建一个新的插件。你应该“注册”你的小部件。如下所示。
add_action( 'elementor/widgets/widgets_registered', [ $this, 'init_widgets' ] );
你应该激活你的新插件。祝好运。
https://stackoverflow.com/questions/55407988
复制相似问题