Support » Fixing WordPress » How to “If is echo HTML / PHP / HTML”

  • Resolved iamcracks

    (@iamcracks)


    I’m trying to IF IS some code that includes html/php/html. However, I’m having an issue.

    The php is being displayed outside of, and BEFORE the first html. And the first html includes the div structure, so it’s breaking badly. The source code shows the order as – php/html/html.

    The code block is as follows …..

    <?php if ( is_home()) { echo '<div class="box">Welcome to ' . bloginfo('name') . ' website</div>' ; } 
    
       elseif (is_single()) { 	echo '<div class="box">Single Page Message</div>' ; }
    
       else { echo '<div class="box">Generic Message</div>' ; }
    			?>

    I know where it’s going wrong, but I don’t know how to rectify it, or the correct ‘ ” echo protocol in such a case.

Viewing 5 replies - 1 through 5 (of 5 total)
  • esmi

    (@esmi)

    Forum Moderator

    Try:

    <?php if ( is_home()) { echo '<div class="box">Welcome to ' . get_bloginfo('name') . ' website</div>' ; } 
    
       elseif (is_single()) { 	echo '<div class="box">Single Page Message</div>' ; }
    
       else { echo '<div class="box">Generic Message</div>' ; }

    With most of the templates tags they are meant to output something directly not be included into other code. But wordpress usually has the same function with get_ prefixed that will return the value instead of echoing it allowing it to be used in your other code.

    Good Resource: Template_Tags

    I tried.
    I tested.
    I thank you.

    Thanks to both: jeremyclark13 and esmi.

    Hi Guys,

    I am having a similar problem, I am trying to get a loop to check if there is a certain meta tag in a post and if there is, then echo certain information. Unfortunately it keeps breaking. Any Ideas?

    <?php query_posts( 'post_type=page' ); ?>
           <?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
    			<?php
    			$key = "when";
    			$club_when = get_post_meta($post->ID, $key, true);
    			if (!$club_when){
    			echo "";
    			}else{
    			echo "<p>" . get_post_meta($post->ID, 'when',true); . "<br/>
    				<a href='" . the_permalink(); . "'>" . the_title(); . "</a></p>";
    
    				}
    			?>

    Thanks

    Matt

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to “If is echo HTML / PHP / HTML”’ is closed to new replies.