php - 将帖子中的自定义元框更改为页面
问题描述
我根据这个播放列表在自定义帖子中开发了 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;
}
//verify slug value
$post_slug = "book";
if($post_slug != $post->post_type){
return;
}
//save value to db filed
$pub_tel = '';
if(isset($_POST['txtPhoneNum'])){
$pub_tel = sanitize_text_field($_POST['txtPhoneNum']);
}
else{
$pub_tel = '';
}
update_post_meta($post_id, "telNo", $pub_tel);
$pub_email = '';
if(isset($_POST['txtEmail'])){
$pub_email = sanitize_text_field($_POST['txtEmail']);
}
else{
$pub_email = '';
}
update_post_meta($post_id, "email", $pub_email);
$pub_hours = '';
if(isset($_POST['txtHours'])){
$pub_hours = sanitize_text_field($_POST['txtHours']);
}
update_post_meta($post_id, "hourofOps", $pub_hours);
}
?>
有人可以给我一个解决方案,在页面而不是帖子中显示元框。
解决方案
代替
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');
有了这个
add_action('add_meta_boxes', 'wpl_owt_register_metabox_cpt');
function wpl_owt_register_metabox_cpt()
{
global $post;
if(!empty($post))
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-contact.php' )
{
add_meta_box(
'owt-cpt-id', // $id
'Contact Details', // $title
'wpl_owt_book_function', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
并替换这个
$post_slug = "book";
if($post_slug != $post->post_type){
return;
}
有了这个。
$post_slug = "page";
if($post_slug != $post->post_type){
return;
}
希望这会成功。
推荐阅读
- vue.js - Vue-i18n setLocale
- sql - SQL Server 以 20 个块为单位处理临时表行
- go - 类型从超集切换到子集接口类型
- linux - 我的 shell 脚本有什么问题?
- python - 试图从字符串列表中删除项目但得到错误 list.remove(x): x not in list
- java - 为什么需要在 long 和 float 数据类型的末尾添加“F”和“L”后缀?
- javascript - 从 1 到 20 的数字(不要使用数组)
- java - 从通过反射调用的主方法获取 System.out.println 的值
- php - 雄辩的选择使用了错误的类?
- javascript - 从 URL 和历史记录中删除锚点