Am I missing an escape for one of these quotes?

I’m not entirely sure why this isn’t working. But this piece of code does not work.

<?php
foreach ( $gallery_ids as $gallery ) {
    echo '<div class="tgallery" rel="'.$gallery['gid'].'"><?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?></div>';
}
?>

I was guessing that maybe I’m putting the wrong quotes in the wrong place.

Read More

All the parts seperately work, as in :

  1. I can display the 'gid' value with echo $gallery['gid']
  2. I can make the div tags appear with the appropriate rel
  3. I can, by itself, make <?php echo do_shortcode("[nggallery id=3]"); ?> work.

I just can’t make the entire thing appear together.

Related posts

Leave a Reply

6 comments

  1. You’re mixing interpolated php and html, placing "<?php echo" inside what’s already php.

    <div class="tgallery" rel="<?php echo $gallery['gid'];?>">
    <?php echo do_shortcode('[nggallery id="'.$gallery['gid'].'"]'); ?>
    </div>
    
  2. Why you put <?php ?> inside your echo ?

    <?php
    foreach ( $gallery_ids as $gallery ) 
    {
        echo '<div class="tgallery" rel="'.$gallery['gid'].'">'.do_shortcode('[nggallery id='.$gallery['gid'].']').'</div>';
    }
    ?>
    
  3. The issue

    Pick either string concatenation or opening/closing PHP for HTML. You cannot combine both as you have done above.

    echo '<div class="tgallery" rel="'.$gallery['gid'].'">
             <?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?>
          </div>';
    

    The second line of code above does not belong inside a string as code in between the <?php ... ?> will not be parsed by PHP when it is contained in a string.

    Solutions

    Concatenation

    I have fixed your code to use concatenation below:

    foreach ( $gallery_ids as $gallery ) {
        $shortcode = do_shortcode("[nggallery id={$gallery['gid']}]");
        echo '<div class="tgallery" rel="' . $gallery['gid'] . '">' . $shortcode . '</div>';
    }
    

    Opening and closing PHP

    This is how you would do it using PHP “templating”:

    <?php foreach($gallery_ids as $gallery ): ?>
        <div class="tgallery" rel="<?php echo $gallery['gid']; ?>">
            <?php echo do_shortcode("[nggallery id={$gallery['gid']}]"); ?>
        </div>
    <?php endforeach; ?>
    
  4. You are already “in” php, so your opening tag is causing the problem:

    <?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?>
    

    It should be something like:

    echo '<div class="tgallery" rel="'.$gallery['gid'].'">' . do_shortcode('[nggallery id='.$gallery['gid'].']') . '</div>';
    
  5. <?php 
    foreach ( $gallery_ids as $gallery ) { 
        echo "<div class="tgallery" rel="{$gallery["gid"]}">". do_shortcode("[nggallery id="{$gallery["gid"]}"]") ."</div>";
    } 
    ?> 
    
  6. try this….

    <?php
    foreach ( $gallery_ids as $gallery ) {
        echo '<div class="tgallery" rel="'.$gallery['gid'].'">'.do_shortcode("[nggallery id=".$gallery['gid']."]").'</div>';
    }
    ?>
    

    you have inside a php statment

    Try Notepad then it will colour code your php code, so you can clearly see what quotes etc you have wrong