因此,我在一个post类型中添加了一个输入字段。
我想做的是在这个字段中放入一个链接(google.com、facebook.com、任何链接),并在前端将其输出到一个按钮中。
下面是我的按钮。我希望它在我单击按钮时转到我在输入字段上放置的链接。
<a href="" id="btn-book-now" class="btn-custom"><?php echo __('Purchase Now','Vierra'); ?></a>
这是代谢盒的代码:
<?php
add_action( 'add_meta_boxes', 'cart_meta_box_add' );
function cart_meta_box_add() {
add_meta_box( 'cart-meta-box-id', 'Put cart Here!', 'cart_meta_box_cb', 'Room', 'side', 'high' );
}
function cart_meta_box_cb() {
wp_nonce_field( basename( __FILE__ ), 'cart_meta_box_nonce' );
$value = get_post_meta(get_the_ID(), 'cart_key', true);
$html = '<label>cart: </label><input type="text" name="cart" value="'.$value.'"/>';
echo $html;
}
add_action( 'save_post', 'cart_meta_box_save' );
function cart_meta_box_save( $post_id ){
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( !isset( $_POST['cart_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['cart_meta_box_nonce'], basename( __FILE__ ) ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
if( isset( $_POST['cart'] ) )
update_post_meta( $post_id, 'cart_key', esc_attr( $_POST['cart'], $allowed ) );
}
?>
谢谢你的帮助!
这与管理员中使用的函数相同:get_post_meta()
。假设按钮在循环内打印*
:
<?php
if( $field = get_post_meta( get_the_ID(), 'cart_key', true ) ) {
?>
<a href="<?php echo $field; ?>" id="btn-book-now" class="btn-custom">
<?php echo __('Purchase Now','Vierra'); ?>
</a>
<?php
}
?>
*
否则,您将使用$post->ID
而不是get_the_ID()