在页面上以自定义帖子类型显示元框

时间:2015-05-19 作者:dea

我有个问题。我正在尝试使用自定义数据库表向自定义帖子类型(插件中)添加一个元框textarea字段。meta框显示在自定义帖子类型中,保存和更新,但只是没有在页面(前端)上显示数据,我就是搞不懂。它显示文章的标题和内容,但不显示输入的数据。以下是我的meta box代码:

    //show metabox in post editing page
    add_action(\'add_meta_boxes\', \'bsp_add_metabox\' );

    function bsp_add_metabox() {
   // add_meta_box( $id, $title, $callback, $screen, $context, $priority );
  add_meta_box(\'bsp_mother_tongue\', \'Mother language\',\'bsp_mother_tongue_handler\', \'badge\',\'normal\');
   }
   //showing the meta box on admin page
  function bsp_mother_tongue_handler($post){

$value = get_post_custom($post->ID);
?>
<!--<label for="bsp_text_meta">Mother language </label><br />-->
<textarea id="bsp_text_meta" name="bsp_text_meta" class="widefat" style="overflow:auto; resize:none" rows="15"><?php if(isset($value[\'bsp_text_meta\'])) {echo $value[\'bsp_text_meta\'][0]; } ?></textarea>
//check if user can edit post
if( !current_user_can( \'edit_posts\' ) ) {
    return;  
}

if( isset($_POST[\'bsp_text_meta\'] )) {
    update_post_meta($post_id, \'bsp_text_meta\', esc_attr($_POST[\'bsp_text_meta\']));
}
}
badge是自定义帖子类型(slug)的名称。

1 个回复
最合适的回答,由SO网友:Wali Hassan 整理而成

如果未使用多个键(&A);价值观,我会这样尝试

// This function adds a meta box with a callback function of my_metabox_callback()
function add_my_meta_box() {
     $var1 = \'this\';
     $var2 = \'that\';
     add_meta_box( 
           \'metabox_id\',
           \'Metabox Title\',
           \'my_metabox_callback\',
           \'page\',
           \'normal\',
           \'low\', 
           array( \'foo\' => $var1, \'bar\' => $var2)
      );
}

// $post is an object containing the current post (as a $post object)
// $metabox is an array with metabox id, title, callback, and args elements. 
// The args element is an array containing your passed $callback_args variables.

function my_metabox_callback ( $post, $metabox ) {
     echo \'Last Modified: \' . $post->post_modified;        // outputs last time the post was modified
     echo $metabox[\'args\'][\'foo\'];                         // outputs \'this\'
     echo $metabox[\'args\'][\'bar\'];                         // outputs \'that\'
     echo get_post_meta( $post->ID, \'my_custom_field\', true ); // outputs value of custom field
}
您还可以使用\\u内容过滤器向single添加任何内容。php

示例代码:

function tsi_source_link($content) {
 if ( is_single() ) {
   global $post;
   $source_link = get_post_meta ($post->ID, \'syndication_permalink\', true);
   if ($source_link) {
    $content .= \'<div id="tsi-source-link" style="text-align:right;"><a href="\' . $source_link . \'" target="_blank">Read more ...</a></div>\';
}
}
return $content;
}
add_action(\'the_content\',\'tsi_source_link\');

结束