Создание пользовательских полей и мета-блоков - PullRequest
0 голосов
/ 01 октября 2019

Код, над которым я работаю, возвращается, но не возвращает значение для телефонного номера

Я пытаюсь создать персонализированную запись для офисов

Я что-то упустил, и яне вижу этого.

function locations_footer() {
    $args = array('post_type'=>'locations',);
    $location_loop = new WP_Query($args);
    if($location_loop->have_posts()):
        while($location_loop->have_posts()):
            $location_loop->the_post();
            $locations = get_post_meta($post->ID,'location_fields',true);
            $telephone = $locations['telephone'];
            $title = get_the_title();
            $content = get_the_content();
            echo'Title:'.$title.'<br>Telephone:'.$telephone.'<br>Content:'.$content.'';
        endwhile;
    endif;
    wp_reset_postdata();
}
add_action('wp_footer','locations_footer');

function locations_post(){
    register_post_type('locations', array(
        'labels' => array(
            'name'=>__('Locations'),
        ),
        'public' => true,
        'hierarchical' => true,
        'has_archive' => true,
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
        ),
        'taxonomies' => array(
            'post_tag',
            'category',
        )
    ));
    register_taxonomy_for_object_type('category','locations');
    register_taxonomy_for_object_type('post_tag','locations');
}
add_action('init','locations_post');


function locations_meta_box(){
    add_meta_box(
        'locations_fields_meta_box',//$id
        'Locations',//$title
        'show_locations_fields_meta_box',//$callback
        'locations',//$screen
        'normal',//$context
        'high'//$priority
    );
}
add_action('add_meta_boxes','locations_meta_box');


function show_locations_fields_meta_box(){
    global $post;
    $locations = get_post_meta($post->ID,'locations_fields',true);
    ?>
    <input type="hidden" name="locations_meta_box_nonce"value="<?php echo wp_create_nonce(basename(__FILE__)); ?> ">
    <p>
        <labelfor="locations_fields[text]">Phone</label><br>
        <input type="text" name="locations_fields[telephone]" id="locations_fields[telephone]" class="regular-text" value="<?php echo $locations['telephone'];?>">
    </p>
    <?php

}
add_action('add_meta_boxes','show_locations_fields_meta_box');

function save_locations_fields_meta($post_id) {
    if(!wp_verify_nonce($_POST['locations_meta_box_nonce'],basename(__FILE__))) {
        return$post_id;
    }
    if(defined('DOING_AUTOSAVE')&&DOING_AUTOSAVE) {
        return$post_id;
    }
    if('page'===$_POST['post_type']) {
        if(!current_user_can('edit_page',$post_id)) {
            return $post_id;
        }
        else if(!current_user_can('edit_post',$post_id)) {
            return $post_id;
        }
    }
    $old = get_post_meta($post_id,'locations_fields',true);
    $new = $_POST['locations_fields'];
    if($new&&$new!==$old) {
        update_post_meta($post_id,'locations_fields',$new);
    } elseif(''===$new && $old) {
        delete_post_meta( $post_id,'locations_fields',$old );
    }
}
add_action('save_post','save_locations_fields_meta');

Ответы [ 2 ]

0 голосов
/ 02 октября 2019

В этой функции:

function locations_footer() {
    $args = array('post_type'=>'locations',);
    $location_loop = new WP_Query($args);
    if($location_loop->have_posts()):
        while($location_loop->have_posts()):
            $location_loop->the_post();
            $locations = get_post_meta($post->ID,'location_fields',true);
            $telephone = $locations['telephone'];
            $title = get_the_title();
            $content = get_the_content();
            echo'Title:'.$title.'<br>Telephone:'.$telephone.'<br>Content:'.$content.'';
        endwhile;
    endif;
    wp_reset_postdata();
}
add_action('wp_footer','locations_footer');

в этой строке, по-видимому, отсутствует "s" $locations = get_post_meta($post->ID,'location_fields',true); должно быть $locations = get_post_meta($post->ID,'locations_fields',true); также для $post->ID, убедитесь, что значение действительно, если не можетхочу попробовать это: https://developer.wordpress.org/reference/functions/get_the_id/

0 голосов
/ 01 октября 2019

Попробуйте этот код. Я прокомментировал эту строку //add_action('add_meta_boxes','show_locations_fields_meta_box');. Это добавит 2 мета-поля.

, а также изменится в сохранении почтового индекса.

function locations_footer() {
    $args = array('post_type'=>'locations',);
    $location_loop = new WP_Query($args);
    if($location_loop->have_posts()):
        while($location_loop->have_posts()):
            $location_loop->the_post();
            $locations = get_post_meta($post->ID,'location_fields',true);
            $telephone = $locations['telephone'];
            $title = get_the_title();
            $content = get_the_content();
            echo'Title:'.$title.'<br>Telephone:'.$telephone.'<br>Content:'.$content.'';
        endwhile;
    endif;
    wp_reset_postdata();
}
add_action('wp_footer','locations_footer');

function locations_post(){
    register_post_type('locations', array(
        'labels' => array(
            'name'=>__('Locations'),
        ),
        'public' => true,
        'hierarchical' => true,
        'has_archive' => true,
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
        ),
        'taxonomies' => array(
            'post_tag',
            'category',
        )
    ));
    register_taxonomy_for_object_type('category','locations');
    register_taxonomy_for_object_type('post_tag','locations');
}
add_action('init','locations_post');


function locations_meta_box(){
    add_meta_box(
        'locations_fields_meta_box',//$id
        'Locations',//$title
        'show_locations_fields_meta_box',//$callback
        'locations',//$screen
        'normal',//$context
        'high'//$priority
    );
}
add_action('add_meta_boxes','locations_meta_box');


function show_locations_fields_meta_box(){
    global $post;
    $locations = get_post_meta($post->ID,'locations_fields',true);
    ?>
    <input type="hidden" name="locations_meta_box_nonce"value="<?php echo wp_create_nonce(basename(__FILE__)); ?> ">
    <p>
        <label for="locations_fields[text]">Phone</label><br>
        <input type="text" name="locations_fields[telephone]" id="locations_fields[telephone]" class="regular-text" value="<?php echo $locations['telephone'];?>">
    </p>
    <?php

}
//add_action('add_meta_boxes','show_locations_fields_meta_box');

function save_locations_fields_meta($post_id) {


    if(defined('DOING_AUTOSAVE')&&DOING_AUTOSAVE) {
        return$post_id;
    }
    if('page'===$_POST['post_type']) {
        if(!current_user_can('edit_page',$post_id)) {
            return $post_id;
        }
        else if(!current_user_can('edit_post',$post_id)) {
            return $post_id;
        }
    }

    $old = get_post_meta($post_id,'locations_fields',true);
    $new = $_POST['locations_fields'];
     update_post_meta($post_id,'locations_fields',$new);

}
add_action('save_post_locations','save_locations_fields_meta');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...