• I am getting an error say there is an unexpected ‘{‘. What I’m trying to do is say:

    If the page has a featured image, use it in this div background

    else

    is any page or post, use this array.

    Any ideas as to what I am doing wrong?

    <?php if ( is_page() ) { ?>
    
    <?php global $post; ?>
    			<?php
    			$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
    			?>
    			<div style="background: url(<?php echo $src[0]; ?> ) !important; min-height: 100%; background-size: cover !important; background-position: center !important; margin: auto; padding: 20px 0; max-width: 1000px;">
    
    <?php } else ( is_page() || is_single() )  { ?>
    
    	<?php
    		$homeDiv = '<div id="wrapper-home">';
    		$homedueDiv = '<div id="wrapper-home2">';
    		$hometreDiv = '<div id="wrapper-home3">';
    		$allDivs = array();
    
    		array_push($allDivs, $homeDiv, $homedueDiv, $hometreDiv);
    
    		$randomNumber = rand(0, count($allDivs) - 1);
    
    		echo $allDivs[$randomNumber];
    	?>	
    
    <?php } ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • It took me a little while to see it too, but…

    When you have an if()/else statement, the else can’t have any conditions. Because you’ve added a conditional check after the else it breaks because that’s not allowed to be there. If you want that you need something like this:

    if($conditon_1) {
    
    } elseif ($conditon2) {
    
    }
    Thread Starter timpan

    (@timpan)

    Thanks for the reply.

    So set up the variables of condition_1, etc. and in theory this should work. Makes sense. I’ll give it a try tomorrow.

    You can still have the same conditions that you do now, that’s no problem. You just need to make it elseif() instead of else.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Another if/else issue’ is closed to new replies.