简体   繁体   English

如何在WordPress中隐藏自定义控件?

[英]How To Hide A Customizer Control in WordPress?

I know contextual controls hide if for example the user navigates from the viewer certain pages. 我知道上下文控件会隐藏,例如,如果用户从查看器中导航某些页面。

But how can I achieve hiding a control under this scenario: 但是在这种情况下如何实现隐藏控件:

There is a type Select Control with an array of options from 1 to 6, Default being 1. 有一种类型为Select Control的选项,包含从1到6的选项数组,默认值为1。

Then there are 6 type Text controls underneath. 然后,下面有6个类型的Text控件。 I want those controls (except for the first one) to be hidden on load and shown depending on what the user chooses from the Select control. 我希望那些控件(第一个控件除外)在加载时隐藏并根据用户从Select控件中选择的内容进行显示。

In other words the Select control determines the number of Text controllers the user would see. 换句话说,“选择”控件确定用户将看到的“文本”控制器的数量。

I tried some JQuery which I am not very good at and the #customize-control_dadada... id's just would not obey. 我尝试了一些我不太擅长的JQuery,并且#customize-control_dadada ... id不会服从。

I can't find any info on this matter, all I find is information about the contextual controls which are useful but do not seem what I need for this matter. 我找不到有关此问题的任何信息,我发现的只是有关上下文控件的信息,这些信息很有用,但似乎并不需要我解决此问题。

Any ideas? 有任何想法吗? thank you in advance. 先感谢您。

I was having the same issue as you. 我和你有同样的问题。 Try surrounding the #customize-control selector in a document.ready. 尝试将#customize-control选择器包含在document.ready中。 For example: 例如:

(function($) {
    $( function() {
        if ($('#customize-control-my-control').length) {
            // Do stuff
        }
    });
})(jQuery)

Put this javascript in a script that is queued on the admin side. 将此javascript放在在管理端排队的脚本中。 To enqueue the script add this to your functions.php: 要使脚本入队,请将其添加到您的functions.php中:

add_action( 'admin_enqueue_scripts', 'admin_enqueue_scripts');

function admin_enqueue_scripts() {
    wp_register_script( 'admin-script', get_template_directory() . '/js/admin.js', array('jquery'), '1.0.0' );
    wp_enqueue_script('admin-script');
}

The above assumes the script is located in the current theme (get_template_directory). 上面假设脚本位于当前主题(get_template_directory)中。 For plugin use: WP_PLUGIN_DIR . 对于插件使用:WP_PLUGIN_DIR。 '/my-plugin'; '/我-插件';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM