未拉入Meta Box值

时间:2014-08-28 作者:dannyw24

下面是在我的CPT命名客户端上创建我的metabox的代码,它显示得很好,似乎在单击update时保留了输入的值。问题在于在页面上显示元框中保存的值。

我尝试了在循环内和循环外输出数据get_the_ID() and $post_id

我试过调试<?php var_dump($link); ?> 在前端显示string(0) ""

在循环中输出meta-box值

// Get Meta data
$link = get_post_meta(get_the_ID(), \'link_url\', true); ?>
<li><a href="<?php echo $link; ?>"><?php the_post_thumbnail(\'clients\'); ?></a></li>
元框代码

/**
     * Adds a box to the main column on the Post and Page edit screens.
     */
    function portfolio_link_add_meta_box() {

    $screens = array( \'clients\' );

    foreach ( $screens as $screen ) {

        add_meta_box(
            \'portfolio_logo_link\',
            __( \'Logo Link\', \'logo_link_textdomain\' ),
            \'logo_link_meta_box_callback\',
            $screen
        );
    }
}

add_action( \'add_meta_boxes\', \'portfolio_link_add_meta_box\' );

function logo_link_meta_box_callback( $post ) {

    // Add an nonce field so we can check for it later.
    wp_nonce_field( \'logo_link_meta_box\', \'logo_link_meta_box_nonce\' );

    /*
     * Use get_post_meta() to retrieve an existing value
     * from the database and use the value for the form.
     */
    $value = get_post_meta( $post->ID, \'link_url\', true );

    echo \'<label for="link">\';
    _e( \'Add The Logo Link\', \'link_logo_textdomain\' );
    echo \'</label> \';
    echo \'<input type="text" id="link" name="link" value="\' . $value . \'" size="25" />\';
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function logo_link_save_meta_box_data( $post_id ) {

    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( ! isset( $_POST[\'logo_link_meta_box_nonce\'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'logo_link_meta_box_nonce\'], \'logo_link_meta_box\' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don\'t want to do anything.
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user\'s permissions.
    if ( isset( $_POST[\'post_type\'] ) && \'page\' == $_POST[\'post_type\'] ) {

        if ( ! current_user_can( \'edit_page\', $post_id ) ) {
            return;
        }

    } else {

        if ( ! current_user_can( \'edit_post\', $post_id ) ) {
            return;
        }
    }

    /* OK, it\'s safe for us to save the data now. */

    // Make sure that it is set.
    if ( ! isset( $_POST[\'link\'] ) ) {
        return;
    }

    // Sanitize user input.
    $my_data = sanitize_text_field( $_POST[\'link\'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, \'link_url\', $my_data );
}
add_action( \'save_post\', \'logo_link_save_meta_box_data\' );

1 个回复
SO网友:Shail

确保自定义post类型slug是$screens=数组(“clients”)中的clients;

因为我刚刚在本地测试了你的代码,它运行得很好。

结束

相关推荐

显示来自自定义Metabox选择的帖子计数

I was wondering if this is possible. 我有一个自定义的post类型调用项目,它显示一个带有下拉菜单的metabox。在这个下拉菜单中,我列出了几个选项:草稿、打开、挂起和完成。一旦您选择了您想要的字段,我希望只有在所选作者登录时,才能够在前端显示该选项的帖子计数。例如,作者A在4篇不同的帖子上选择完成4次。因此,当作者A登录到该站点时,他会在前端看到一个数字4,并填写了单词。这是我用来显示下拉菜单的内容 array( \'name\'