• Resolved mariostella

    (@mariostella)


    Hi, I am trying to display a div with a picture gallery for some posts only that have a certain tag, and another div with other content for other posts that do not have that tag but have another. However, it is not working.

    This is the query I adapted from https://codex.www.ads-software.com/Template_Tags/get_the_tags (Display code bases on different tag values). I am not using it in the loop. I tried using it in the loop, but it won’t work.

    <?php
    if ($all_the_tags);
    $all_the_tags = get_the_tags();
    foreach($all_the_tags as $this_tag)
    	if ($this_tag->name == "show-gal" ) {
    ?>
    <div>
    <?php
    $gal = get_post_meta($post->ID, 'galleryflash', true);
    $gal = apply_filters('the_content', $gal );
    echo $gal;
    ?>
    
    <?php
    $gal = get_post_meta($post->ID, 'gallerypopup', true);
    $gal = apply_filters('the_content', $gal );
    echo $gal;
    ?>
    
    </div>
    
    <?php 	} else if ($this_tag->name == "no-show-gal" ) { ?>
    
    **ALTERNATE CONTENT HERE**
    
    <?php 	} else {
    		// it's neither, do nothing
    ?>
    		<!-- not tagged as one or the other -->
    <?
    	}
    }
    
    ?>

    The error I get is the following:

    Parse error: syntax error, unexpected ‘}’ in /home/user/public_html/wp-content/themes/theme/single.php on line 176

Viewing 5 replies - 1 through 5 (of 5 total)
  • I think your code should be

    <?php
    if ($all_the_tags){
    	$all_the_tags = get_the_tags();
    	foreach($all_the_tags as $this_tag){
    		if ($this_tag->name == "show-gal" ) {
    ?>
    <div>
    <?php
    $gal = get_post_meta($post->ID, 'galleryflash', true);
    $gal = apply_filters('the_content', $gal );
    echo $gal;
    ?>
    <?php
    $gal = get_post_meta($post->ID, 'gallerypopup', true);
    $gal = apply_filters('the_content', $gal );
    echo $gal;
    ?>
    </div>
    <?php 		} else if ($this_tag->name == "no-show-gal" ) { ?>
    
    **ALTERNATE CONTENT HERE**
    
    <?php 		} else {
    		// it's neither, do nothing
    ?>
    		<!-- not tagged as one or the other -->
    <?		}
    	}
    }?>

    Try that and see if it works.

    Thread Starter mariostella

    (@mariostella)

    Hi tomontoast, thank you for your help
    Unfortunately it does not work. I do not get any errors, but the <div> or alternate content do not show up at all now.

    Does this HAVE TO be used within the loop? Why won’t it work otherwise?

    Yes it has to be in the loop because otherwise wordpress won’t know which post to do get_the_tags() on. You can also use the has_tag() conditional query in the wordpress loop.

    Use the following code:

    <?php while (have_posts()) : the_post(); ?>
    <?php if (has_tag('show-gal')) { ?>
    <div>
    <?php
    $gal = get_post_meta($post->ID, 'galleryflash', true);
    $gal = apply_filters('the_content', $gal );
    echo $gal;
    ?>
    <?php
    $gal = get_post_meta($post->ID, 'gallerypopup', true);
    $gal = apply_filters('the_content', $gal );
    echo $gal;
    ?>
    </div>
    <?php }elseif (has_tag('no-show-gal')) { ?>
    **ALTERNATE CONTENT HERE**
    <?php }else{ ?>
    <!-- not tagged as one or the other -->
    <?php } ?>
    <?php endwhile; ?>

    Thread Starter mariostella

    (@mariostella)

    Thanks a lot tom! It works, and it seems a lighter load on the server too (for as much as I understand of PHP..).

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘php inside php to display gallery conditionally not working’ is closed to new replies.