WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to "If is echo HTML / PHP / HTML" (6 posts)

  1. iamcracks
    Member
    Posted 4 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    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>' ; }
  3. Jeremy Clark
    Moderator
    Posted 4 years ago #

    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

  4. iamcracks
    Member
    Posted 4 years ago #

    I tried.
    I tested.
    I thank you.

  5. iamcracks
    Member
    Posted 4 years ago #

    Thanks to both: jeremyclark13 and esmi.

  6. mtedwards
    Member
    Posted 3 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags