douchongbang6011 2019-01-28 15:47
浏览 23

尝试显示城市列表

First of all, sorry my horrible English =)

I'll try to explain how works everything:

I use this to create 'agreement' to be searched on my website, só the basic is the name, and where that 'agreement' works, for example, states and cities... but I have a problem when I try to select the 'states' and after show the cities to select where my 'agreement' can be used and save to show on the site. But when I select the states, the WordPress just don't show the cities, and this problem happened after some att in WordPress plugins (exemple: Advanced custom field)

And know I have no idea how I can fix this.

Here is my code:

//Load cities - Convenio

function ag_apcef_load_cities_field_choices( $field ) {
    // get selected
    $selected = get_field( 'ag-estado-convenio', $post->ID );
    // reset choices
    $field['choices'] = array();


    if($selected) {
        $args = array(
            'post_type' => 'cities',
            'orderby' => 'title',
            'order' => 'ASC',
            'posts_per_page' => -1,
            'meta_query' => array(
                array(
                   'key'     => 'city_meta_box_state',
                   'value'   => $selected->ID
                )
            )
        );

        $posts = get_posts( $args );
        foreach($posts as $post) {
            $field['choices'][$post->ID] = $post->post_title;
        }
    }

    return $field;
}
add_filter('acf/load_field/name=ag-cidade-convenio', 'ag_apcef_load_cities_field_choices');


 // Admin enqueue - Convenio

function ag_apcef_admin_convenio_enqueue( $hook ) {
    // Allowed post types
    $types = array( 'convenio' );

    if( !in_array( get_post_type(), $types ) )
        return;

    wp_enqueue_script( 'populate-area', get_stylesheet_directory_uri() . '/js/autopopulate-admin.js' );

    wp_localize_script( 'populate-area', 'populate_vars', 
        array(
            'populate_nonce' => wp_create_nonce( 'populate_nonce' ), // Create nonce
        )
    );
}
add_action( 'admin_enqueue_scripts', 'ag_apcef_admin_convenio_enqueue' );


 //Enqueue - Convenio

function ag_apcef_convenio_enqueue() {
  if ( is_page( 'portal-de-vantagens' ) || is_page( 'bem-estar' )) {
    wp_enqueue_script( 'populate-area', get_stylesheet_directory_uri() . '/js/autopopulate.js' );

    wp_localize_script( 'populate-area', 'populate_vars', 
        array(
            'populate_nonce' => wp_create_nonce( 'populate_nonce' ), // Create nonce
            'ajaxurl' => admin_url( 'admin-ajax.php' )
        )
    );
  }
}
add_action( 'wp_enqueue_scripts', 'ag_apcef_convenio_enqueue' );


 //  Load cities by state

function ag_apcef_city_by_state( $selected_state ) {
    // Verify nonce
    if( !isset( $_POST['populate_nonce'] ) || !wp_verify_nonce( $_POST['populate_nonce'], 'populate_nonce' ) )
        die('Permission denied');

    // Get selected state
    $selected_state = $_POST['state'];

    // Populate arr_data
    $arr_data = array();
    if(!empty($selected_state)) {
        $args = array(
            'post_type' => 'cities',
            'orderby' => 'title',
            'order' => 'ASC',
            'posts_per_page' => -1,
            'meta_query' => array(
                array(
                   'key'     => 'city_meta_box_state',
                   'value'   => $selected_state
                )
            )
        );

        $posts = get_posts( $args );
        foreach($posts as $post) {
            $arr_data[] = [
                'key' => $post->ID,
                'value' => $post->post_title
            ];
        }
    }

    return wp_send_json($arr_data);
    die();
}
add_action('wp_ajax_apcef_city_by_state', 'ag_apcef_city_by_state');
add_action('wp_ajax_nopriv_apcef_city_by_state', 'ag_apcef_city_by_state');
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答