If variable is link, echo link

I having issues getting a function to echo, where $lightbox_link1 = get_custom_field('lightbox_link1'). I’m fairly new to PHP.

Below is the defining function:

Read More
// Check for a lightbox link, if it exists, use that as the value. 
// If it doesn't, use the featured image URL from above.
if(get_custom_field('lightbox_link1')) {                            
    $lightbox_link1 = get_custom_field('lightbox_link1');
} else {                            
    $lightbox_link1 = $image_full[0];
}

Echo Function:

<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
     echo '<a href="<?php echo $lightbox_link1; ?>" data-rel="prettyPhoto[<?php echo $post_slug; ?>]"></a>';
} ?>

Related posts

Leave a Reply

1 comment

  1. <?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
    

    should be

    <?php if ($lightbox_link1 == get_custom_field('lightbox_link1')) {
    

    = is used for assignment

    == is used for comparison

    === is used for typesafe comparison

    also you can’t declare <?php ... ?> inside another <?php ... ?>

    to get something like <?php ... <?php ... ?> ... ?>

    take a look at what you did up to here:

     <?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
        echo '<a href="<?php
    

    Instead, using doublequotes in your echo statement will allow for the php variables inside to be parsed, so you could just do

    echo "<a href='{$lightbox_link1}' data-rel='prettyPhoto[{$post_slug}]'></a>";
    

    to get

    <?php if ($lightbox_link1 == get_custom_field('lightbox_link1')) {
         echo "<a href='{$lightbox_link1}' data-rel='prettyPhoto[{$post_slug}]'></a>";
    } ?>