如何以正确的方式关闭我的PHP标签?

时间:2014-03-22 01:56:16

标签: php html

我有一段PHP代码,我需要以正确的方式关闭php标签,但我不知道最好的方法是什么,因为我有html和php混淆了。我已从我不确定的部分删除了标签。

<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>

2 个答案:

答案 0 :(得分:1)

使用此修改后的代码。请参阅代码中需要相应打开/关闭它们的注释。

<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>

答案 1 :(得分:1)

您需要在使用php代码时随时打开和关闭php标记

   <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>