как отобразить содержимое meta_box? - PullRequest
0 голосов
/ 30 апреля 2020

Я беру код от бывшего разработчика. Два типа метас были созданы для типов записей. Вот код:

/* custom meta box */
add_action("add_meta_boxes", "publication_meta_boxes");

function publication_meta_boxes() {
    global $post;
    if ($post->post_type == 'publications') {
        add_meta_box("publication-text-wp-editor", "titre de la publication", "publication_text_wp_e", "publications", "normal", "low");
        add_meta_box("link-wp-box", "référence", "link_wp_box", "publications", "normal", "low");
    }
}

function publication_text_wp_e() {
    global $post;
    $custom = get_post_custom($post->ID);
    $publication_text_wp_e = $custom["publication_text_wp_e"][0];
    wp_editor($publication_text_wp_e, 'publication_text_wp_e');
}

function link_wp_box() {

    global $post;
    $custom = get_post_meta($post->ID, "link_wp_box", true);
    ?>
    <input style="width:100%" placeholder="Link title" type="text" name="link_wp_box[link_title]" value="<?= $custom['link_title'] ?>"/><br/>
    <input style="width:100%" placeholder="Link href" type="text" name="link_wp_box[link_href]" value="<?= $custom['link_href'] ?>"/>
    <?php
}

Как отобразить их в веб-интерфейсе. Я пробовал довольно много вещей, но ничего не отображается.

заранее спасибо

сердечно

Николас

Ответы [ 2 ]

0 голосов
/ 02 мая 2020

Спасибо, но это не работа. Мой полный код функционирует. php

    /* custom post type: "publications" */
add_action('init', 'register_cpt_publications');

function register_cpt_publications() {

    $labels = array(
        'name' => _x('Publications', 'post type general name'),
        'singular_name' => _x('Publication', 'post type singular name'),
        'add_new' => _x('poster une publication', 'contact item'),
        'add_new_item' => __('poster une publication'),
        'edit_item' => __('modifier la publication'),
        'new_item' => __('nouvelle publication'),
        'view_item' => __('voir la publication'),
        'search_items' => __('recherche de publications'),
        'not_found' => __('pas trouvé'),
        'not_found_in_trash' => __('pas trouvé'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'Publications',
        'supports' => array('title', 'thumbnail', 'editor'),
        'public' => true,
        'show_ui' => true,
        //'menu_icon' => get_stylesheet_directory_uri() . '/inc/images/cpt-icon-publications.png',
        'show_in_menu' => true,
        'menu_position' => 22,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'has_archive' => false,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => array('slug' => 'publications'),
        'capability_type' => 'post'
    );

    register_post_type('publications', $args);
}

/* custom meta box */
add_action("add_meta_boxes", "publication_meta_boxes");

function publication_meta_boxes() {
    global $post;
    if ($post->post_type == 'publications') {
        add_meta_box("publication-text-wp-editor", "titre de la publication", "publication_text_wp_e", "publications", "normal", "low");
        add_meta_box("link-wp-box", "référence", "link_wp_box", "publications", "normal", "low");
    }
}

function publication_text_wp_e() {
    global $post;
    $custom = get_post_custom($post->ID);
    $publication_text_wp_e = $custom["publication_text_wp_e"][0];
    wp_editor($publication_text_wp_e, 'publication_text_wp_e');
}

function link_wp_box() {

    global $post;
    $custom = get_post_meta($post->ID, "link_wp_box", true);
    ?>
    <input style="width:100%" placeholder="Link title" type="text" name="link_wp_box[link_title]" value="<?= $custom['link_title'] ?>"/><br/>
    <input style="width:100%" placeholder="Link href" type="text" name="link_wp_box[link_href]" value="<?= $custom['link_href'] ?>"/>
    <?php
}

add_action('save_post', 'publication_save_post');

function publication_save_post() {
    global $post;

    if ($_POST['post_type'] == "publications") {
        if (isset($_POST["publication_text_wp_e"])){
            update_post_meta($post->ID, "publication_text_wp_e", $_POST["publication_text_wp_e"]);
        }
        if (isset($_POST["link_wp_box"])) {
            update_post_meta($post->ID, "link_wp_box", $_POST["link_wp_box"]);
        }

    }
}

function getPostMeta($postID){
    $your_key = 'publication-text-wp-editor';
    $value = get_post_meta($postID, $your_key , true);

    return $value;
}
0 голосов
/ 01 мая 2020

Вы можете создать функцию в плагине или функциях. * Файл 1006 * вашей темы, чтобы получить значения мета-поля сообщения:

function getPostMeta($postID){
    $your_key = 'link_wp_box';
    $value = get_post_meta($postID, $your_key , true);

    return $value;
}

Затем вызвать функцию в вашем сингле. php или где вы хотите отобразить значение.

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