输入字段输出链接

时间:2014-04-13 18:26:45

标签: php html wordpress

所以,我在其中一个帖子类型中添加了一个输入字段。

我要做的是将一个链接(google.com,facebook.com,任何链接)放入此字段,并将其在前端输出到一个按钮。

下面是我的按钮。当我点击按钮时,我希望它转到我在输入字段上放置的链接。

<a href="" id="btn-book-now" class="btn-custom"><?php  echo __('Purchase Now','Vierra'); ?></a>

以下是metabox的代码:

<?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 ) );
}

?>

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

这与管理员使用的功能相同: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()

相关问题