1

I have added a custom meta box to a category that shows up fine. The only purpose of this metabox is to supply a link to the post. I can see that the box works because my test link is saved in the box. Now I am trying to get the value on the front end but having a little trouble.

Here is the metabox:

function cd_meta_box_cb()
{
        global $post;
        $values = get_post_custom( $post->ID );
        $text = isset( $values['link'] ) ? esc_attr( $values['link'][0] ) : '';

        wp_nonce_field( 'tickets_link_nonce', 'meta_box_nonce' );

        ?><label for="link">Tickets Link</label>
      <input type="text" name="link" id="link" value="<?php echo $text; ?>" /><?php
}

add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'tickets_link_nonce' ) ) return;

    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;

         $allowed = array(
        'a' => array( // on allow a tags
            'href' => array() // and those anchors can only have href attribute
        )
    );

        if( isset( $_POST['link'] ) ) {
        update_post_meta( $post_id, 'link', wp_kses( $_POST['link'], $allowed ) );
}

Here is the current version of my code on the front end.

 $tickets = array();
 $tickets = get_post_custom($post->ID); ?>

 <?php if( !empty($tickets['link'])): ?>
      <a href="<?php echo $tickets[0]; ?>" class="tickets_btn">Tickets</a>
 <?php endif; ?>

I have gotten it to display the link in the first post. I have two issues I am trying to figure out.

  1. There are 12 posts in the category and even though the metabox value for post two is empty, it still shows a blank link

  2. I am getting undefined indexes on the other posts.

Here is a print_r on the array

(
    [_thumbnail_id] => Array
        (
            [0] => 1865
            [1] => 1865
        )

    [_edit_last] => Array
        (
            [0] => 1
            [1] => 1
        )

    [_am_city] => Array
        (
            [0] => The Dome
            [1] => The Dome
        )

    [tooltip] => Array
        (
            [0] => Come down to the Dome
        )

    [panels_data] => Array
        (
            [0] => a:0:{}
        )

    [_edit_lock] => Array
        (
            [0] => 1416882347:1
        )

    [ninja_forms_form] => Array
        (
            [0] => 0
        )

    [link] => Array
        (
            [0] => http://testlink.com
        )

    [_am_showdates] => Array
        (
            [0] => <p>27</p>DEC
        )

    [_am_location] => Array
        (
            [0] => The Dome
        )

    [_am_state] => Array
        (
            [0] => Detroit, Mi
        )

)

I am trying to get the [link] => http://testlink.com

  • Jamie, please add all your relevant code directly to your question. 1.) If the pastes are removed, then the integrity of your question is compromised 2.) It is really really frustrating switching between three different screens to read one post. Keep your question as readable as possible to keep the interest of possible help :-) – Pieter Goosen Nov 25 '14 at 4:34
0

Try this:

<a href="<?php echo $tickets['link'][0]; ?>" class="tickets_btn">Tickets</a>

Its better to use esc_url() to display URL. Like:

<?php echo esc_url($tickets['link'][0]); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.