我发誓我已经尝试了每一个教程,并查看了每一个堆栈溢出线程,以找出我做错了什么。。。我正在为教堂创建一个自定义wordpress后端,其中包括一个用于上传布道的地方(我的CPT)。。。我很容易地制作了元框,但我无法保存它们。
下面是我的函数中每个部分的代码。php文件。
创建CPT:
add_action( \'init\', \'create_post_type\' );
function create_post_type() {
register_post_type( \'Sermon\',
array(
\'labels\' => array(
\'name\' => __( \'Sermons\' ),
\'slug\' => __(\'sermon\'),
\'singular_name\' => __(\'Sermon\'),
\'edit_item\' => __(\'Edit Sermon\'),
\'add_new_item\' => __(\'Add New Sermon\'),
\'add_new\' => __(\'New Sermon\'),
\'not_found\' => __(\'Looks like you have not uploaded any sermons! Get started by clicking "Add New Sermon" in the menu bar.\')
),
\'public\' => true,
\'has_archive\' => true,
\'menu_icon\' => \'dashicons-book-alt\',
\'menu_position\' => 5,
\'supports\' => array (\'title\', \'editor\', \'revisions\', \'date\', \'thumbnail\')
)
);
}
创建元框:
function sermon_video_settings_markup($object) {
wp_nonce_field(basename(__FILE__), "sermon_video_meta_nonce");
?>
<style>
.sermon_video_settings_table {
width: 100%;
}
tr.sermon_video_settings_rows {
width: 50%;
}
.sermon_video_text_input, .sermon_audio_link {
width: 100%;
}
</style>
<table class="sermon_video_settings_table">
<tr class="sermon_video_settings_rows">
<td>
<label>Sermon Vimeo Link:</label>
</td>
<td>
<label>Sermon Youtube Link:</label>
</td>
</tr>
<tr class="sermon_video_settings_rows">
<td>
<input class="sermon_video_text_input" type="text" name="sermon_vimeo_link" value="<?php echo get_post_meta($object->ID, "sermon_vimeo_link", true); ?>">
</td>
<td>
<input class="sermon_video_text_input" type="text" name="sermon_youtube_link" value="<?php echo get_post_meta($object->ID, "sermon_youtube_link", true); ?>">
</td>
</tr>
<tr>
<td>
<label>Sermon Audio File:</label>
</td>
<td>
</td>
</tr>
<tr>
<td>
<input class="sermon_audio_link" type="text" name="sermon_audio_link" value="<?php echo get_post_meta($object->ID, "sermon_audio_link", true); ?>">
</td>
</tr>
</table>
<?php
}
function sermon_video_settings_meta(){
add_meta_box("sermon_video_settings", "Sermon Video Settings", "sermon_video_settings_markup", "sermon", "normal", "high", null);
}
add_action("add_meta_boxes", "sermon_video_settings_meta");
最后,这里是我的“保存/编辑/检索”代码,它似乎不起作用。。。
function save_custom_meta_box($post_id, $post, $update)
{
if (!isset($_POST["sermon_video_meta_nonce"]) || !wp_verify_nonce($_POST["sermon_video_meta_nonce"], basename(__FILE__)))
return $post_id;
if(!current_user_can("edit_post", $post_id))
return $post_id;
if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
return $post_id;
$slug = "sermon";
if($slug != $post->post_type)
return $post_id;
$sermon_vimeo_link = "";
$meta_box_dropdown_value = "";
if(isset($_POST["sermon_vimeo_link"]))
{
$meta_box_text_value = $_POST["sermon_vimeo_linkt"];
}
update_post_meta($post_id, "sermon_vimeo_link", $meta_box_text_value);
if(isset($_POST["sermon_youtube_link"]))
{
$meta_box_dropdown_value = $_POST["sermon_youtube_link"];
}
update_post_meta($post_id, "sermon_youtube_link", $meta_box_dropdown_value);
}
add_action("save_post", "save_custom_meta_box", 10, 3);
任何帮助都将是惊人的。我已经为此工作了太多小时,我真的开始感到沮丧了。我对wordpress的这种深度定制还相当陌生,所以如果答案相当简单,我也不会感到惊讶。以下教程是我用来指导创建元框和保存函数的大部分教程,仅供参考。
http://www.sitepoint.com/adding-custom-meta-boxes-to-wordpress/
最合适的回答,由SO网友:setterGetter 整理而成
这是您的临时生成和检查。。。
您也没有保存音频链接,我清理了元数据的设置(清理URL并修复条件)
这对我来说在本地有效
<?php
add_action( \'init\', \'create_post_type\' );
function create_post_type() {
register_post_type( \'Sermon\',
array(
\'labels\' => array(
\'name\' => __( \'Sermons\' ),
\'slug\' => __( \'sermon\' ),
\'singular_name\' => __( \'Sermon\' ),
\'edit_item\' => __( \'Edit Sermon\' ),
\'add_new_item\' => __( \'Add New Sermon\' ),
\'add_new\' => __( \'New Sermon\' ),
\'not_found\' => __( \'Looks like you have not uploaded any sermons! Get started by clicking "Add New Sermon" in the menu bar.\' )
),
\'public\' => true,
\'has_archive\' => true,
\'menu_icon\' => \'dashicons-book-alt\',
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'revisions\', \'date\', \'thumbnail\' )
)
);
}
function sermon_video_settings_markup( $object ) {
wp_nonce_field( \'sermon_nonce_action\', \'sermon_nonce_field\' );
?>
<style>
.sermon_video_settings_table {
width: 100%;
}
tr.sermon_video_settings_rows {
width: 50%;
}
.sermon_video_text_input, .sermon_audio_link {
width: 100%;
}
</style>
<table class="sermon_video_settings_table">
<tr class="sermon_video_settings_rows">
<td>
<label>Sermon Vimeo Link:</label>
</td>
<td>
<label>Sermon Youtube Link:</label>
</td>
</tr>
<tr class="sermon_video_settings_rows">
<td>
<input class="sermon_video_text_input" type="text" name="sermon_vimeo_link" value="<?php echo get_post_meta( $object->ID, "sermon_vimeo_link", true ); ?>">
</td>
<td>
<input class="sermon_video_text_input" type="text" name="sermon_youtube_link" value="<?php echo get_post_meta( $object->ID, "sermon_youtube_link", true ); ?>">
</td>
</tr>
<tr>
<td>
<label>Sermon Audio File:</label>
</td>
<td>
</td>
</tr>
<tr>
<td>
<input class="sermon_audio_link" type="text" name="sermon_audio_link" value="<?php echo get_post_meta( $object->ID, "sermon_audio_link", true ); ?>">
</td>
</tr>
</table>
<?php
}
function sermon_video_settings_meta() {
add_meta_box( "sermon_video_settings", "Sermon Video Settings", "sermon_video_settings_markup", "sermon", "normal", "high", null );
}
add_action( "add_meta_boxes", "sermon_video_settings_meta" );
function save_custom_meta_box( $post_id, $post, $update ) {
if ( ! isset( $_POST[\'sermon_nonce_field\'] )
|| ! wp_verify_nonce( $_POST[\'sermon_nonce_field\'], \'sermon_nonce_action\' )
) {
return $post_id;
}
if ( ! current_user_can( "edit_post", $post_id ) ) {
return $post_id;
}
if ( defined( "DOING_AUTOSAVE" ) && DOING_AUTOSAVE ) {
return $post_id;
}
$slug = "sermon";
if ( $slug != $post->post_type ) {
return $post_id;
}
if ( isset( $_POST["sermon_vimeo_link"] ) ) {
$value = esc_url_raw( $_POST["sermon_vimeo_link"] );
update_post_meta( $post_id, "sermon_vimeo_link", $value );
}
if ( isset( $_POST["sermon_youtube_link"] ) ) {
$value = esc_url_raw( $_POST["sermon_youtube_link"] );
update_post_meta( $post_id, "sermon_youtube_link", $value );
}
if ( isset( $_POST["sermon_audio_link"] ) ) {
$value = esc_url_raw( $_POST["sermon_audio_link"] );
update_post_meta( $post_id, "sermon_audio_link", $value );
}
}
add_action( "save_post", "save_custom_meta_box", 10, 3 );