1

I'm using the WP Alchemy class to add custom metaboxes to templates in the admin panel and pull results from those metaboxes into the theme front-end. Here is the relevant code in content-link.php (in the loop - in Twenty Eleven Theme).

<a href="<?php display_url_info(); ?>"><img src="http://localhost/wordpress/wp-content/uploads/2011/08/external-link-red03.png"/></a>   

Right now the link shows just the permalink instead of the url in the metabox. If I hardcode a url in there instead, the custom url shows up on the post. I've verified it being properly stored under _custom_meta in the db (and successfully implemented metabox display in other templates. Here's the value in the db:

a:1:{s:5:"links";a:1:{i:0;a:2:{s:6:"domain";s:25:"http://stackexchange.com/";s:13:"post_link_url";s:48:"http://wordpress.stackexchange.com/questions/ask";}}}

Here's my plugin file:

$basic_metabox = new WPAlchemy_MetaBox (

    array (
    'id' => '_custom_meta',
    'title' => 'Basic Meta',
    'template' => ABSPATH . 'wp-content/themes/beernews-child-theme/custom/basic-meta.php',
    'types' => array('post','link')
    )

);

   add_filter('beernews-child-theme_post', 'display_url_info');

function display_url_info() {

    // usually needed
    global $basic_metabox;

    // get the meta data for the current post
    $basic_metabox->the_meta(); ?>

    <?php if ($basic_metabox->get_the_value('post_link_url')) {

    echo $basic_metabox->the_value('post_link_url');

}

}

2 Answers 2

1

Try changing your display_url_info function to

<?php
add_filter('beernews-child-theme_post', 'display_url_info');

function display_url_info() {
    // usually needed
    global $basic_metabox;
    $basic = $basic_metabox->the_meta(); 

    if($basic['post_link_url']) {
        echo $basic['post_link_url'];
    } else {
        echo 'ERROR : "post_link_url" empty';
    }
}
?>

And not to be a jerk, but I would like to point out that WP Alchemy is not a plugin but rather a PHP Class. I just do not want other people to get confused.

9
  • here's what I see on hovering over the link...localhost/wordpress/><br/><b>Notice</b>: Undefined index: post_link_url in <b>c:/xampp/htdocs/wordpress/wp-content/wp-content/plugins/meta-box-junk/meta-box-junk.php</b> on line <b>229</b><br/>
    – Adam
    Commented Aug 28, 2011 at 0:48
  • I tried adding this but no luck. No idea what I'm doing really.. $basic_url = isset( $basic['post_link_url'] ) ? esc_attr( $basic['post_link_url'] ) : '';
    – Adam
    Commented Aug 28, 2011 at 1:08
  • Hm. Try to do some debugging. (1) Instead of putting it in a function write it directly in your theme. (2) Can you also post where you defined $basic_metabox;
    – Evan Yeung
    Commented Aug 28, 2011 at 14:31
  • Yeah I turned on debug in wp-config. Nothing came up. Not sure what other debugging I can do. I edited my post to add where I defined $basic_metabox. As for implementing some other way, it's the exact same fashion that I'm implementing other boxes...hmmm.
    – Adam
    Commented Aug 29, 2011 at 15:29
  • I want to try one more thing. Change the return to echo. I updated my script in my answer.
    – Evan Yeung
    Commented Aug 29, 2011 at 15:43
0

Not sure why you are using the filter exactly ...

I would do the following in the theme file:

<?php global $basic_metabox; /* this is just needed once */ ?>
<?php if ($basic_metabox->have_value('post_link_url')) { ?>
<a href="<?php $basic_metabox->the_value('post_link_url'); ?>"><img src="http://localhost/wordpress/wp-content/uploads/2011/08/external-link-red03.png"/></a> 
<?php } ?>

Also note that get_the_value() and the_value() are different, the first returning a value and the second echoing out a value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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