gpt4 book ai didi

wordpress - 无法使用自定义帖子类型在 WordPress 中保存元框数据

转载 作者:行者123 更新时间:2023-12-02 22:31:41 26 4
gpt4 key购买 nike

我真正想要的是将元框数据保存为A)作为循环内可访问的全局变量,B)将数据保存到文本框中,以便当用户按下更新时,他们编写的内容会出现在文本框,直到再次更新。目前,我知道它不符合 B) 的标准,并且我不确定它是否可以作为循环中的全局变量进行访问。有什么帮助吗?

add_action( 'add_meta_boxes', 'testimonial_text_box' );

function testimonial_text_box() {
add_meta_box(
'testimonial_text_box',
__( 'Testimonial Text:', 'myplugin_textdomain' ),
'testimonial_text_box_content',
'testimonial',
'normal',
'high'
);
}

function testimonial_text_box_content( $post ) {
$values = get_post_custom( $post->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : ”;
$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
$check = isset( $values['my_meta_box_check'] ) ? esc_attr( $values['my_meta_box_check'][0] ) : ”;

wp_nonce_field( plugin_basename( __FILE__ ), 'testimonial_text_box_content_nonce' );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="testimonial_text">';
_e("Text body of the testimonial:", 'myplugin_textdomain' );
echo '</label> ';
echo '<br/>';
echo '<textarea align="top" id="testimonial_text" name="testimonial_text" value="'.esc_attr($value).'" style="width:100%;height:200px;margin:5px -20px 3px 0;" /></textarea>';
}

add_action( 'save_post', 'testimonial_text_box_save' );
function testimonial_text_box_save( $post_id ) {

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;

if ( !wp_verify_nonce( $_POST['testimonial_text_box_content_nonce'], plugin_basename( __FILE__ ) ) )
return;

if ( 'testimonial' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
$testimonial_text = $_POST['testimonial_text'];
update_post_meta( $post_id, 'testimonial_text', $testimonial_text );
}

最佳答案

您的代码存在问题:

  1. 不必要的障碍(与当前目标无关):

    $values = get_post_custom( $post->ID );  
    $text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : ”;
    $selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
    $check = isset( $values['my_meta_box_check'] ) ? esc_attr( $values['my_meta_box_check'][0] ) : ”;
  2. 您正在保存testimonial_text:

    update_post_meta( $post_id, 'testimonial_text', $testimonial_text );

    但是获取_my_meta_value_key:

    $value = get_post_meta( $post->ID, '_my_meta_value_key', true );

    get_ 更改为 testimonial_text

  3. textarea 没有,内容位于打开/关闭标记内:

    echo '<textarea id="testimonial_text" name="testimonial_text" />'.esc_attr($value).'</textarea>';
  4. 钩子(Hook)save_post需要2个参数:

    add_action( 'save_post', 'testimonial_text_box_save', 10, 2 );
    function testimonial_text_box_save( $post_id, $post ) { /* code */ }
  5. 有一个不起作用的 if/else 应该类似于:

    if ( 'testimonial' !== $post->post_type )        
    return;

    if ( !current_user_can( 'edit_post' ) )
    return;

要在前端使用帖子元,只需使用get_post_meta()

关于wordpress - 无法使用自定义帖子类型在 WordPress 中保存元框数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17816757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com