Engin
Engin

Reputation: 135

How to close my php tags in the correct way?

I have a piece of php code and I need to close the php tags in the right way, but I am not sure what the best way is because I have html and php mixed up. I have removed the tags from the part I am not sure about.

<div id="fphItems">
            $i = 1; 
            query_posts( 'posts_per_page=4&cat=3' );
            if ( have_posts() ) { 
                while ( have_posts() ) {
                    the_post();
                    if ( $i < 4 ) { 
                        echo '<div class="fphItem">';
                    } 
                    else {
                        echo '<div class="fphLastItem">';
                    }
                    if ( has_post_thumbnail() ) {
                        the_post_thumbnail();
                        <div class="fphItemTitle">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                        </div>
                    }
                }
            }
            else {
                <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
            }
            <?php wp_reset_query(); 
            </div>

Upvotes: 2

Views: 223

Answers (2)

Use this modified code. See the comments inside the code where you need to open/close them appropriately.

<div id="fphItems">
    <?php //<--- Open Here
    $i = 1;
    query_posts( 'posts_per_page=4&cat=3' );
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            if ( $i < 4 ) {
                echo '<div class="fphItem">';
            }
            else {
                echo '<div class="fphLastItem">';
            }
            if ( has_post_thumbnail() ) {
                the_post_thumbnail();
                ?> <!-- Close here -->
                <div class="fphItemTitle">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </div>
            <?php // Open here again
            }
        }
    }
    else {
        echo "<p>";
        echo('Sorry, no posts matched your criteria.');
        echo "</p>";
    }
    wp_reset_query(); ?> <!-- Close here -->
</div>

Upvotes: 1

Fabio
Fabio

Reputation: 23510

You need to open and close php tag anytime you use php code

   <div id="fphItems">
   <?php
        $i = 1; 
        query_posts( 'posts_per_page=4&cat=3' );
        if ( have_posts() ) { 
            while ( have_posts() ) {
                the_post();
                if ( $i < 4 ) { 
                    echo '<div class="fphItem">';
                } 
                else {
                    echo '<div class="fphLastItem">';
                }
                if ( has_post_thumbnail() ) {
                    the_post_thumbnail(); 
     ?>
                    <div class="fphItemTitle">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                    </div>
     <?php
                }
            }
        }
        else {
     ?>
            <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
     <?php
        }
     ?>
        <?php wp_reset_query(); ?>
        </div>

Upvotes: 1

Related Questions