【问题标题】:Settings for custom wordpress widget not showing up自定义 wordpress 小部件的设置未显示
【发布时间】:2019-08-19 20:05:47
【问题描述】:

我正在尝试为子主题扩展 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')
    );
}

?>

如何让我的自定义小部件的设置字段以与默认图库小部件相同的方式工作?如果我添加额外的字段,它会搞砸吗?

【问题讨论】:

    标签: wordpress widget custom-wordpress-pages


    【解决方案1】:

    您的问题是关于在 wordpress 上创建一个新插件。 您应该“注册”您的小部件。如下所示。

    add_action( 'elementor/widgets/widgets_registered', [ $this, 'init_widgets' ] );  
    

    你应该激活你的新插件。 祝你好运。

    【讨论】:

      猜你喜欢
      • 2011-12-25
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2013-04-06
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      相关资源
      最近更新 更多