简体   繁体   中英

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:

// 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>';
} ?>
<?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>";
} ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM