获取发布元检索错误的值

时间:2013-04-20 作者:user1632018

我创建了一个带有文本框的自定义元框,用户可以在其中输入自定义值作为附加标识标记。我认为我是对的,但当我尝试检索值时,它会给我post ID,而不是输入的自定义值。

如果帖子太长了,我很抱歉,但我想彻底一点。要获取post meta,我使用以下方法:

     <?php $meta_values = get_post_meta( the_ID(), newtheme_section_id); ?>
我正在添加我用来创建元框的代码,我希望有人能帮我一把。谢谢

/**
 * Sets up custom section ID meta box in admin post and edit pages.
 *
 * @since newtheme 1.0
 */
add_action( \'load-post.php\', \'section_id_meta_box_setup\' );
add_action( \'load-post-new.php\', \'section_id_meta_box_setup\' );

/* Meta box setup function. */
function section_id_meta_box_setup() {

    /* Add meta box on the \'add_meta_boxes\' hook. */
    add_action( \'add_meta_boxes\', \'newtheme_section_id_meta_boxes\' );

/* Save post meta on the \'save_post\' hook. */
add_action( \'save_post\', \'newtheme_save_section_id_meta\', 10, 2 );
}

/* Create meta box to be displayed on the post editor screen. */
function newtheme_section_id_meta_boxes() {

    add_meta_box(
        \'newtheme-section-id\',          // Unique ID
        esc_html__( \'Custom Section ID\', \'ID\' ),        // Title
        \'newtheme_section_id_meta_box\',     // Callback function
        \'post\',                 // Admin page (or post type)
        \'side\',                 // Context
        \'default\'                   // Priority
    );
}

/* Display the post meta box. */
function newtheme_section_id_meta_box( $object, $box ) { ?>

    <?php wp_nonce_field( basename( __FILE__ ), \'newtheme_section_id_nonce\' ); ?>

    <p>
        <label for="newtheme-section-id"><?php _e( "You can give your section a name and it will appear in the menus. ", \'Optional\' ); ?></label>
        <br />
        <input class="widefat" type="text" name="newtheme-section-id" id="newtheme-section-id" value="<?php echo $text; ?>" size="30" />
    </p>
<?php }

/* Save the meta box\'s post metadata. */
function newtheme_save_section_id_meta( $post_id, $post ) {

    /* Verify the nonce before proceeding. */
    if ( !isset( $_POST[\'newtheme_section_id_nonce\'] ) || !wp_verify_nonce( $_POST[\'newtheme_section_id_nonce\'], basename( __FILE__ ) ) )
        return $post_id;

    /* Get the post type object. */
    $post_type = get_post_type_object( $post->post_type );

    /* Check if the current user has permission to edit the post. */
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    /* Get the posted data and sanitize it for use as an HTML class. */
    $new_meta_value = ( isset( $_POST[\'newtheme-section-id\'] ) ? sanitize_html_class( $_POST[\'newtheme-section-id\'] ) : \'\' );

    /* Get the meta key. */
    $meta_key = \'newtheme_section_id\';

    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key, true );

    /* If a new meta value was added and there was no previous value, add it. */
    if ( $new_meta_value && \'\' == $meta_value )
        add_post_meta( $post_id, $meta_key, $new_meta_value, true );

    /* If the new meta value does not match the old value, update it. */
    elseif ( $new_meta_value && $new_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key, $new_meta_value );

    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( \'\' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );
编辑:我正在尝试显示用户在页面上输入的新ID标记值,以便每篇文章都显示用户输入的自定义ID标记。

下面是我尝试使用的代码。

    <?php $post_id = get_the_ID(); ?>
    <?php $value = get_post_meta($post_id, \'newtheme_section_id\'); ?>
    <id="<?php $value; ?>">
似乎当我这样做时,我得到的是一个空白值。因为它没有返回任何东西。我确信它正在保存帖子页面中的数据,因为在编辑帖子时,自定义标记会出现在输入框中。我到底做错了什么?

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

请花些时间阅读法典。the_ID echos content. 这意味着该ID永远不会传递给get_post_meta. 只是echo安装到位。来自同一法典页:

注意:此函数显示帖子的ID,要返回ID,请使用get\\u the\\u ID()。

因此,使用get_the_ID 相反

第二个问题。。。引用字符串。正如您所编写的,PHP将尝试查找constant 已命名newtheme_section_id. 我认为这不是故意的。

$meta_values = get_post_meta( get_the_ID(), \'newtheme_section_id\' );
如果该代码在循环中,它应该可以工作。

结束