Php 如何使用div'正确包装我的列;s

Php 如何使用div'正确包装我的列;s,php,wordpress,loops,while-loop,Php,Wordpress,Loops,While Loop,我有以下代码将我的列包装在div中: <div id="blogwrapper"> <div id="blogs"> <?php //enable pagination on static pages and blog pages $col = 1; //Let's create first column /*Let's add pagination to post page and static page*/ if (

我有以下代码将我的列包装在div中:

<div id="blogwrapper">  
<div id="blogs">    
    <?php //enable pagination on static pages and blog pages

    $col = 1; //Let's create first column
    /*Let's add pagination to post page and static page*/
    if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
    } else {
    $paged = 1;
    }



$args = array(
    /* Add whatever you need here - see http://codex.wordpress.org/Class_Reference/WP_Query */    
     'paged' => $paged,

);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>        

<?php if ($col == 1) echo '<div class="row">';//If column 1 create first row ?>
<?php if ($col == 2) echo '<div class="row2">';//If column 2 create second row ?>

    <div <?php post_class('col'.$col); ?> id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <div class="entry"> 
                <div class="featured_img">
                <?php the_post_thumbnail();
      echo '<div class="featured_caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';?>
     </div><!--/featured_img-->
        <?php  // let's enable more link on pages...
        global $more;
        $more = 0;
        ?>
            <?php the_content(); ?> 
            <div class="clear"></div>               
            <div class="custom_fields"><?php the_meta(); ?></div><br/>
            <p class="postmetadata">

            <?php _e('Filed under&#58;','override'); ?> <?php the_category(', ') ?> <?php _e('by','override'); ?> <?php  the_author(); ?><br/><?php the_tags('Tags:', ', ', '<br />'); ?>
            <?php _e('Posted on:&nbsp;','override'); ?><?php the_time('l, F jS, Y'); ?><br/>

            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>
    </div>

    <?php /*Enable Two Column Layout*/
        if($col==1) {
        $col=2;
        echo "</div>";
        }
        else if($col==2)  {
        $col=1;
         echo "</div>";
        }

    endwhile; ?>
    <div class="clear"></div>
    <div class="navigation">

    <?php
    global $wp_query;
    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, $paged ),
    'total' => $wp_query->max_num_pages
    ) );
    ?>
</div>
<?php endif; ?> 
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>



</div><!--/blogs--> 
</div><!--/blogswrapper-->  

使用



我现在觉得自己好笨。。。我只把
放在循环下面,它就成功了!现在我的工作代码如下所示:

    <?php /* Template name: Two Columns*/   
    ?>  

 <?php get_header(); ?>

 <?php get_sidebar(); ?>

 <?php get_sidebar('secondary'); ?>

<div id="blogwrapper">  
<div id="blogs">    

    <?php //enable pagination on static pages and blog pages

    $col = 1; //Let's create first column
    /*Let's add pagination to post page and static page*/
    if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
    } else {
    $paged = 1;
    }



$args = array(
    /* Add whatever you need here - see http://codex.wordpress.org/Class_Reference/WP_Query */    
     'paged' => $paged,

);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>        



    <div <?php post_class('col'.$col); ?> id="post-<?php the_ID(); ?>">
            <!--NOTICE THAT THIS CODE IS NOW BELOW!!!-->

    <?php if ($col == 1) echo '<div class="row">';//If column 1 create first row ?>
    <?php if ($col == 2) echo '<div class="row2">';//If column 2 create second row ?>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <div class="entry"> 
                <div class="featured_img">
                <?php the_post_thumbnail();
      echo '<div class="featured_caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';?>
     </div><!--/featured_img-->
        <?php  // let's enable more link on pages...
        global $more;
        $more = 0;
        ?>
            <?php the_content(); ?> 
            <div class="clear"></div>               
            <div class="custom_fields"><?php the_meta(); ?></div><br/>
            <p class="postmetadata">

            <?php _e('Filed under&#58;','override'); ?> <?php the_category(', ') ?> <?php _e('by','override'); ?> <?php  the_author(); ?><br/><?php the_tags('Tags:', ', ', '<br />'); ?>
            <?php _e('Posted on:&nbsp;','override'); ?><?php the_time('l, F jS, Y'); ?><br/>

            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>
    </div>

    <?php /*Enable Two Column Layout*/
        if($col==1) {
        $col=2;
        echo "</div>";
        }
        else if($col==2)  {
        $col=1;
         echo "</div>";
        }

    endwhile; ?>
    <div class="clear"></div>
    <div class="navigation">

    <?php
    global $wp_query;
    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, $paged ),
    'total' => $wp_query->max_num_pages
    ) );
    ?>
</div>
<?php endif; ?> 
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>



</div><!--/blogs--> 
</div><!--/blogswrapper-->  

    <?php /* Template name: Two Columns*/   
    ?>  

 <?php get_header(); ?>

 <?php get_sidebar(); ?>

 <?php get_sidebar('secondary'); ?>

<div id="blogwrapper">  
<div id="blogs">    

    <?php //enable pagination on static pages and blog pages

    $col = 1; //Let's create first column
    /*Let's add pagination to post page and static page*/
    if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
    } else {
    $paged = 1;
    }



$args = array(
    /* Add whatever you need here - see http://codex.wordpress.org/Class_Reference/WP_Query */    
     'paged' => $paged,

);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>        



    <div <?php post_class('col'.$col); ?> id="post-<?php the_ID(); ?>">
            <!--NOTICE THAT THIS CODE IS NOW BELOW!!!-->

    <?php if ($col == 1) echo '<div class="row">';//If column 1 create first row ?>
    <?php if ($col == 2) echo '<div class="row2">';//If column 2 create second row ?>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <div class="entry"> 
                <div class="featured_img">
                <?php the_post_thumbnail();
      echo '<div class="featured_caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';?>
     </div><!--/featured_img-->
        <?php  // let's enable more link on pages...
        global $more;
        $more = 0;
        ?>
            <?php the_content(); ?> 
            <div class="clear"></div>               
            <div class="custom_fields"><?php the_meta(); ?></div><br/>
            <p class="postmetadata">

            <?php _e('Filed under&#58;','override'); ?> <?php the_category(', ') ?> <?php _e('by','override'); ?> <?php  the_author(); ?><br/><?php the_tags('Tags:', ', ', '<br />'); ?>
            <?php _e('Posted on:&nbsp;','override'); ?><?php the_time('l, F jS, Y'); ?><br/>

            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>
    </div>

    <?php /*Enable Two Column Layout*/
        if($col==1) {
        $col=2;
        echo "</div>";
        }
        else if($col==2)  {
        $col=1;
         echo "</div>";
        }

    endwhile; ?>
    <div class="clear"></div>
    <div class="navigation">

    <?php
    global $wp_query;
    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, $paged ),
    'total' => $wp_query->max_num_pages
    ) );
    ?>
</div>
<?php endif; ?> 
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>



</div><!--/blogs--> 
</div><!--/blogswrapper-->