首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WordPress设置API可重复字段

WordPress设置API可重复字段
EN

WordPress Development用户
提问于 2020-05-06 12:46:10
回答 2查看 475关注 0票数 0

我正试图找到一种方法,在我创建的自定义管理页面中添加可重复的字段。

代码:

代码语言:javascript
复制
  /*WordPress Menus API.*/
    function add_new_menu_items()
    {
        add_menu_page(
            "Theme Options", 
            "Theme Options",
            "manage_options",
            "theme-options", 
            "theme_options_page", 
            "", 
            100 
        );

    }

    function theme_options_page()
    {
        ?>
        
            
            
            Theme Options

有什么方法使advertising_code输入可重复吗?

谢谢

EN

回答 2

WordPress Development用户

发布于 2020-05-10 17:17:11

这不是最好的解决方案,但也许它对你自己的解决方案是有帮助的。

代码语言:javascript
复制
function display_ads_form_element()
{
    // get the total number of "advertising codes" from the database
    // the default value is 1
    $codes  = array('1' => '' );
    if( get_option('advertising_code') !== FALSE ) {
        $codes = get_option('advertising_code');
    }
    ?>

    
    

    

         $code): ?>

            
             square brackets means the data will send as array  // ?>
             is only an easy way that the js can detect the last key  // ?>
            

        


    

    

        function add_new_ad_code() {

            // detect the last li element
            const last_li = document.getElementById('advertising_code_list').lastElementChild;

            // get the listKey from the input field
            const new_key = parseInt(last_li.childNodes[0].dataset.listkey) + 1;

            // create the new list tag
            const li_element = document.createElement('li');

            // create the new input tag for the new advertising code
            const input_element = document.createElement('input');

            // set the type
            input_element.type = "text";

            // set the name attribute
            input_element.name = "advertising_code[" + new_key +  "]";

            // set empty value
            input_element.value = "";

            // add the new key as data attribute
            input_element.dataset.listkey = new_key;

            // add the new advertising code to the list element
            li_element.appendChild(input_element);

            // add the list element to the list
            document.getElementById('advertising_code_list').appendChild(li_element);
        }
票数 1
EN

WordPress Development用户

发布于 2020-05-06 20:21:13

在PHP打开标签(如 )后在页面顶部添加,并将其称为需要$advertising_code;的地方。

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/365980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档