将帖子中的自定义元框更改为页面

我根据这个播放列表在自定义帖子中开发了 Meta Box 插件。(我对此做了一些自定义)在这里,他使用插件创建了一个自定义帖子,并对该相关帖子执行所有元框操作。


它工作正常。但我的要求是在页面中显示这个元框。


这是我的代码。


metabox.php(插件代码)


<?php


function wpl_owt_custom_init_cpt(){

    $args = array(

        'public' => true,

        'label' => 'Books',

        'supports' => array('title' , 'editor' , 'author' , 'thumbnail' , 'excerpt' , 'comments')

    );

    register_post_type('book' , $args);

   }

   add_action('init', 'wpl_owt_custom_init_cpt');




 function wpl_owt_register_metabox_cpt(){

        //custom post type

        add_meta_box("owt-cpt-id" , "Contact Details" , "wpl_owt_book_function" , "book" , "normal" , "high");

   }

   add_action("add_meta_boxes_book" , "wpl_owt_register_metabox_cpt");



   /**********Callback function for metabox at custom post type book******************/


 function wpl_owt_book_function( $post ) {

    //echo "<p>Custom metabox for custom post type</p>";


    define("_FILE_", "_FILE_");


    wp_nonce_field( basename(_FILE_), "wp_owt_cpt_nonce");


    echo "<label for='txtPhoneNum'>Phone</label><br>";

    $phone_num = get_post_meta($post->ID, "telNo" , true);

    echo "<input type ='tel' name = 'txtPhoneNum' value = '" . $phone_num . "'' placeholder = 'Phone Number' /><br><br>";


    echo "<label for='txtEmail'>Email</label><br>";

    $email = get_post_meta($post->ID, "email" , true);

    echo "<input type ='email' name = 'txtEmail' value = '" . $email . "'' placeholder = 'Email Address' /><br><br>";


    echo "<label for='txtHours'>Hours of Operation</label><br>";

    $hours = get_post_meta($post->ID, "hourofOps" , true);

    echo "<input type ='text' name = 'txtHours' value = '" . $hours . "'' placeholder = 'Working Hours' /><br><br>";

}


add_action("save_post" , "wpl_owt_save_metabox_data" , 10 , 2);


function wpl_owt_save_metabox_data($post_id, $post){


    //verify nonce

    if(!isset($_POST['wp_owt_cpt_nonce']) || !wp_verify_nonce($_POST['wp_owt_cpt_nonce'], basename(_FILE_))){

        return $post_id;

    }


侃侃尔雅
浏览 81回答 2
2回答

幕布斯6054654

代替function wpl_owt_custom_init_cpt(){&nbsp; &nbsp; $args = array(&nbsp; &nbsp; &nbsp; &nbsp; 'public' => true,&nbsp; &nbsp; &nbsp; &nbsp; 'label' => 'Books',&nbsp; &nbsp; &nbsp; &nbsp; 'supports' => array('title' , 'editor' , 'author' , 'thumbnail' , 'excerpt' , 'comments')&nbsp; &nbsp; );&nbsp; &nbsp; register_post_type('book' , $args);&nbsp; &nbsp;}&nbsp; &nbsp;add_action('init', 'wpl_owt_custom_init_cpt');有了这个add_action('add_meta_boxes', 'wpl_owt_register_metabox_cpt');function wpl_owt_register_metabox_cpt(){&nbsp; &nbsp; global $post;&nbsp; &nbsp; if(!empty($post))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);&nbsp; &nbsp; &nbsp; &nbsp; if($pageTemplate == 'page-contact.php' )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add_meta_box(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'owt-cpt-id', // $id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Contact Details', // $title&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'wpl_owt_book_function', // $callback&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'page', // $page&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'normal', // $context&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'high'); // $priority&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}并替换这个$post_slug = "book";&nbsp; &nbsp; if($post_slug != $post->post_type){&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }有了这个。$post_slug = "page";&nbsp; &nbsp; if($post_slug != $post->post_type){&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }希望这会成功。

SMILET

请看如下:<?phpfunction add_custom_meta_box(){&nbsp; &nbsp; $screens = ['page'];&nbsp; &nbsp; foreach ($screens as $screen) {&nbsp; &nbsp; &nbsp; &nbsp; add_meta_box(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'meta_box_id',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Unique ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Custom Meta Box Title',&nbsp; // Box title&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'callback_function',&nbsp; // Content callback, must be of type callable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $screen&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Post type&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}add_action('add_meta_boxes', 'add_custom_meta_box');function callback_function($post){&nbsp; &nbsp;/*Do something*/}
打开App,查看更多内容
随时随地看视频慕课网APP