• I’m trying to implement the code here to pull an image from a tag. Taken from codex here
    There seems to be a parse error on line 21, the last curly brace before the closing php. Can someone see what it wrong in the code here?

    <?php
    if ($all_the_tags);
    $all_the_tags = get_the_tags();
    foreach($all_the_tags as $this_tag) {
    	if ($this_tag->name == "sometag" ) {
    ?>
    
    <p>SOME HTML CODE <img src="someimage.jpg"></p>
    
    <?php 	} else if ($this_tag->name == "someothertag" ) { ?>
    
    <p>SOME OTHER HTML CODE <img src="someotherimage.jpg"></p>
    
    <?php 	} else {
    		// it's neither, do nothing
    ?>
    		<!-- not tagged as one or the other -->
    <?
    	}
    }
    }
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try:

    <?php
    if ($all_the_tags) {
    	$all_the_tags = get_the_tags();
    	foreach($all_the_tags as $this_tag) {
    		if ($this_tag->name == "sometag" ) {
    ?>
    
    <p>SOME HTML CODE <img src="someimage.jpg"></p>
    
    <?php}
    elseif ($this_tag->name == "someothertag" ) { ?>
    
    <p>SOME OTHER HTML CODE <img src="someotherimage.jpg"></p>
    
    <?php }
    else {
    		// it's neither, do nothing
    ?>
    		<!-- not tagged as one or the other -->
    <?php
    }
    ?>
    Thread Starter houseofstrauss

    (@houseofstrauss)

    Thank-you esmi for your help here. I am now getting a parse error on line 10 <?php}

    Please note: this code is in the loop as required.
    Thanks for any further help.

    Try:

    <?php
    if ($all_the_tags) {
    	$all_the_tags = get_the_tags();
    	foreach($all_the_tags as $this_tag) {
    		if ($this_tag->name == "sometag" ) { ?><p>SOME HTML CODE <img src="someimage.jpg"></p><?php}
    		elseif ($this_tag->name == "someothertag" ) { ?><p>SOME OTHER HTML CODE <img src="someotherimage.jpg"></p><?php }
    	}
    }
    else {
    	// it's neither, do nothing ?>
    	<!-- not tagged as one or the other -->
    	<?php
    }
    ?>
    Thread Starter houseofstrauss

    (@houseofstrauss)

    Thanks esmi, that seems to have done the trick! I think elseif does not like to be separted to 2 words… else if

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Tag to image’ is closed to new replies.