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

SIMPLE Echo Custom Field into PHP WordPress

  • SOLVED

Hi, trying to echo ‘cf_url’ inside ‘$url’ … wondering if you could assist me?



<?php echo get_post_meta($post->ID, 'cf_url', true);?>

<img src=" <?php
$url = '<strong>CF URL HERE</strong>';
echo wcdco ($url, $width);
?>" alt="<?php the_title(); ?>" >



Thanks!

Answers (3)

2011-01-11

Michael Fields answers:

Here is the most secure way to do this:

<?php print '<img src="' . esc_url( get_post_meta( $post->ID, 'cf_url', true ) ) . '" ' . wcdco( $url, $width ) . ' alt="' . esc_attr( get_the_title() ) . '" />'; ?>

It is very important to escape output before using it.


West Coast Design Co. comments:

Hi, this is actually using a plug in for generating thumbnails ... I just wanted to replace the source with custom field data.


Michael Fields comments:

So do you want to print the value of the custom field or the url from Binary Moon's plugin? Looks like you have 2 different url's in your code.


Michael Fields comments:

Oh, are you storing a url in the the custom field that you want a screen shot taken of?


West Coast Design Co. comments:

LOL Yes ... wanted to keep some secrecy lol


Michael Fields comments:

Can't fool me :)

Give this a try:

/* URL to take a screenshot of. */
$url = get_post_meta( $post->ID, 'cf_url', true );

/* Image source generated via BM Shots plugin. */
$src = esc_url( bm_mshot( $url, $width ) );

/* Get title for attribute. */
$alt = esc_attr( get_the_title() );

/* Print the results to the browser if the url is proper. */
if ( ! empty( $src ) ) {
print '<img src="' . $src . '" alt="' . $alt . '" />';
}


Michael Fields comments:

BTW, I do the exact same thing on my site. Love this plugin!

2011-01-11

Rashad Aliyev answers:

Hello,

do you like this?



<?php echo get_post_meta($post->ID, 'cf_url', true); ?>

2011-01-11

Jonah Schulte answers:

A couple of things:

1.) What does the 'wcdco' function look like?

2.) If you want $url to equal the value of the 'cf_url' custom field and print that out, you could just do this:


<?php
$url = get_post_meta($post->ID, 'cf_url', true);
?>

<img src="<?php echo $url; ?>" alt="<?php the_title(); ?>" />



If you want to use the wcdco function (I'm not sure what that does), I would guess you could do this?


<img src="<?php echo wcdco($url, $width); ?>" alt="<?php the_title(); ?>" />



West Coast Design Co. comments:

Hi, this is the full code:



<img src=" <?php
$url = '<strong>cf_url_here</strong>';
$width = 300;
echo bm_mshot ($url, $width);
?>" alt="<?php the_title(); ?>" width="300" height="225" >



<strong>cf_url_here</strong> being my custom field data.


Jonah Schulte comments:

This does what you are asking for, as I posted above:


<?php
$url = get_post_meta($post->ID, 'cf_url', true);
?>

<img src="<?php echo $url; ?>" alt="<?php the_title(); ?>" />


You could make that more concise and do it all on one line, too, as Michael pointed out:


<?php print '<img src="' . esc_url( get_post_meta( $post->ID, 'cf_url', true ) ) . '" ' . wcdco( $url, $width ) . ' alt="' . esc_attr( get_the_title() ) . '" />'; ?>