Post Meta

自定义字段容器用于使用附加字段扩展后期编辑屏幕。 每个帖子的字段数据作为帖子元单独存储(请参阅 add_post_meta())。

容器位置

有关位置选项的更多信息,请参见 add_meta_box()函数。

Context

应该显示容器的页面部分('normal'(默认)、'advanced'、'side' 或 'carbon_fields_after_title')

->set_context('normal')

Priority

容器应显示的上下文中的优先级(“high”(默认)、“core”、“default”或“low”)

->set_priority('high')

访问字段值

要访问字段值,您需要使用函数 carbon_get_post_meta( $id, $name ),其中:

参数 描述
$id 在输入值的位置Post ID。
$name 要检索的字段的字段名称模式。
<!-- Simple field -->
<p>Article was published in: <?php echo carbon_get_post_meta( get_the_ID(), 'crb_location' ); ?></p>

<!-- Complex field -->
<?php 
$slides = carbon_get_post_meta( get_the_ID(), 'crb_slides' );
if ( $slides ) {
    foreach ( $slides as $slide ) {
        echo $slide['image'];
    }
}
?>

您还可以使用 carbon_get_the_post_meta( $name )The Loop 访问当前帖子的值。

<p>Article was published in: <?php echo carbon_get_the_post_meta( 'crb_location' ); ?></p>

<?php $slides = carbon_get_the_post_meta( 'crb_slides' ); ?>

保存后,会调用钩子,它允许您在保存后钩子附加功能。 它接受参数,即正在更新的帖子的 ID。 示例:carbon_fields_post_meta_container_saved $post_id

add_action( 'carbon_fields_post_meta_container_saved', 'crb_after_save_event' );
function crb_after_save_event( $post_id ) {
    if ( get_post_type( $post_id ) !== 'crb_event' ) {
        return false;
    }

    $event_date = carbon_get_post_meta( $post_id, 'crb_event_date' );
    if ( $event_date ) {
        $timestamp = strtotime( $event_date );
        update_post_meta( $post_id, '_crb_event_timestamp', $timestamp );
    }
}