如何使用div正确包装我的列?

时间:2013-05-22 17:15:24

标签: 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 ( 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-->  

但问题出在这里:我正在处理

(div class =“row”和div class =“row2”)(取决于我的阅读)后端设置,例如,如果我的阅读设置是博客页面显示7个帖子)输出如下:
http://www.vasinternetposao.com/testall/wrap_problem.png
但是如果我设置博客页面显示2个帖子我正在“仅”两个空div。
http://www.vasinternetposao.com/testall/wrap2.png
那么有没有人知道如何在没有所有那些 div的情况下正确包装我的列? THX !!

2 个答案:

答案 0 :(得分:0)

使用

<?php if ($col == 1): ?> <div class="row"> 

<?php elseif($col==2): ?>

<div class="row2">

<?php endif; ?>

..........

答案 1 :(得分:0)

我现在感觉很蠢......我只把<?php if ($col == 1) echo '<div class="row">';//If column 1 create first row ?>放在循环下面而且它有效!所以现在我的工作代码看起来像这样:

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