Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Meta field link in shortcodes WordPress

  • SOLVED

I'm working with a meta field on a custom template for showing my users a image with a download link to files for the post. I'm trying to achieve this by adding shortcodes in the template and it's almost working.

I added the post meta field value to my template like this:
<?php
// Get custom post meta field values
$link = get_post_meta($post->ID, 'last_ned_filer', true);
?>


and tried to echo the field to the download image url in the shortcode:
function download_files() {
return '<a href="<?php echo $link; ?"><img src="http://mediesjef.no/wp-content/uploads/2011/05/last-ned.png" width="590" height="138" /></a>';
}
add_shortcode('download_files', 'download_files');


But that didn't work.

Do you have any ideas how I can get my link from the meta field to the image?

Answers (3)

2011-05-19

Caroline Keim answers:

It looks as if you're never running the shortcode in your template. You'll need to do something like this in your template -

<?php
$link = get_post_meta($post->ID, 'last_ned_filer', true);
echo do_shortcode('[download_files]$link[/download_files]');
?>

Your function should be something like

function download_files( $atts, $content = null ) {
return '<a href="' . $content . '"><img src="http://mediesjef.no/wp-content/uploads/2011/05/last-ned.png" width="590" height="138" /></a>';
}

add_shortcode('download_files', 'download_files');


Caroline Keim comments:

It looks as if you're never running the shortcode in your template. You'll need to do something like this in your template -

<?php
$link = get_post_meta($post->ID, 'last_ned_filer', true);
echo do_shortcode('[download_files]$link[/download_files]');
?>

Your function should be something like

function download_files( $atts, $content = null ) {
return '<a href="' . $content . '"><img src="http://mediesjef.no/wp-content/uploads/2011/05/last-ned.png" width="590" height="138" /></a>';
}

add_shortcode('download_files', 'download_files');


dolekjole comments:

Hi, Caroline!

I did ran the shortcode in my template, and with the shortcode function you gave me I was able to solve my problem. I used your function and added this to my template:

<?php echo do_shortcode(do_shortcode("[private][download_files]$link [/download_files][/private]")); ?>

Perfect!
Thanks

2011-05-19

Julian Lannigan answers:

function download_files() {
global $post;
$link = get_post_meta($post->ID, 'last_ned_filer', true);
return '<a href="<?php echo $link; ?"><img src="http://mediesjef.no/wp-content/uploads/2011/05/last-ned.png" width="590" height="138" /></a>';
}
add_shortcode('download_files', 'download_files');


That should do it.


Julian Lannigan comments:

The problem you were having was that the variables you were using were not in the same scope with each other. You have to make the global variable $post available inside the download_files function. Then get the download link. And you do all this inside the shortcode function.


Julian Lannigan comments:

@Peter Michael ;)

2011-05-19

Peter Michael answers:

Try this:

function download_files() {
global $post;
$link = get_post_meta($post->ID, 'last_ned_filer', true);
return '<a href="<?php echo $link; ?"><img src="http://mediesjef.no/wp-content/uploads/2011/05/last-ned.png" width="590" height="138" /></a>';

}
add_shortcode('download_files', 'download_files');


Peter Michael comments:

Ha, Julian was faster ;-)