1

I'm using the following code to display several custom fields on my posts in WordPress, but the URL field doesn't work as I want and I don't know how to solve it:

$destino_web = get_post_meta( get_the_ID(), 'web', true);
$destino_telefono = get_post_meta( get_the_ID(), 'telefono', true);
$destino_precio = get_post_meta( get_the_ID(), 'precio', true);
$destino_horario = get_post_meta( get_the_ID(), 'horario', true);

if( ! empty( $destino_web ) ) { echo '<p><strong>Web:</strong><a href="' . $web . '"><?php get_the_title(); ?></a></p>'; }
if( ! empty( $destino_telefono ) ) { echo '<p><strong>Telefono:</strong> ' . $destino_telefono . '</p>'; }
if( ! empty( $destino_precio ) ) { echo '<p><strong>Precio:</strong> ' . $destino_precio . '</p>'; }
if( ! empty( $destino_horario ) ) { echo '<p><strong>Horario:</strong> ' . $destino_horario . '</p>'; }

I would like to display the post title with my custom URL.

1 Answer 1

0

You are using get_the_title(), which needs to be echoed out. However, you have it inside the string. Also, you might want to use isset() rather than !empty().

Try this:

if( isset( $destino_web ) && $destino_web !== '') { echo sprintf('<p><strong>Web:</strong><a href="%s">%s</a></p>', $destino_web, get_the_title() ); }
3
  • Thanks for your help. Now it prints the title but linking to the current post, even if the web field is empty. Commented Nov 17, 2014 at 7:36
  • Sorry, I had copied the $web var into my code, which you have not defined. I changed it to $destino_web to match the rest of your code. Also, I added a check to make sure $destino_web is not set to an empty string.
    – SkyShab
    Commented Nov 17, 2014 at 15:26
  • Hey, no problem. Glad I could help. Would you mind accepting my answer? Thanks!
    – SkyShab
    Commented Nov 18, 2014 at 14:13

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.