请帮我一栏一栏地打赢这场战役

时间:2012-01-04 作者:Zlatomir

我需要有关此代码的帮助。如何更改代码,使一个类别的帖子按此方案安排。scheme picture

    <?php query_posts(\'cat=3&amp;showposts=1\'); ?>
    <?php while(have_posts()) : ?>
    <?php
    $postcount++;
    if( ($postcount % 2) == 0 ) : // skip \'even\' posts
        $wp_query->next_post();
    else :
    ?>
    <?php the_post(); ?>

                        <?php if ( function_exists( \'get_the_image\' ) ) { get_the_image( array( \'custom_key\' => array( \'post_thumbnail\' ), \'default_size\' => \'full\',\'image_class\' => \'alignleft\', \'width\' => \'120\', \'height\' => \'120\' ) ); } ?>


    <?php endif; ?>
    <?php endwhile; ?>
    </div>

    <?php $postcount = 0; rewind_posts(); ?>

    <div class="even-column">
    <?php query_posts(\'cat=3&amp;showposts=2\'); ?>
    <?php while(have_posts()) : ?>
    <?php
    $postcount++;
    if( ($postcount % 2) != 0 ) : // skip \'odd\' posts
        $wp_query->next_post();
    else :
    ?>
    <?php the_post(); ?>

                        <?php if ( function_exists( \'get_the_image\' ) ) { get_the_image( array( \'custom_key\' => array( \'post_thumbnail\' ), \'default_size\' => \'full\', \'image_class\' => \'alignleft\', \'width\' => \'60\', \'height\' => \'60\' ) ); } ?>

    <?php endif; ?>
    <?php endwhile; ?>
    </div> 

2 个回复
最合适的回答,由SO网友:jocken 整理而成

您不应多次使用query\\u帖子。这将产生错误。你必须使用wp_query.

以下是wordpress codex中的内容:

query_posts( is meant for altering the main loop. 使用query\\u posts()后,将更改与post相关的全局变量和模板标记。在调用query\\u posts()后调用的条件标记也将被更改-这可能是也可能不是预期的结果。

http://codex.wordpress.org/Function_Reference/query_posts

<?php $query_one = new WP_Query(\'cat=3&amp;showposts=1\'); ?>
<?php while($query_one->have_posts()) : ?>
<?php
$postcount++;
if( ($postcount % 2) == 0 ) : // skip \'even\' posts
    $wp_query->next_post();
else :
?>
<?php $query_one->the_post(); ?>

                    <?php if ( function_exists( \'get_the_image\' ) ) { get_the_image( array( \'custom_key\' => array( \'post_thumbnail\' ), \'default_size\' => \'full\',\'image_class\' => \'alignleft\', \'width\' => \'120\', \'height\' => \'120\' ) ); } ?>


<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata();  // Important to reset it when done ?> 
</div>

<?php $postcount = 0; rewind_posts(); ?>

<div class="even-column">
<?php $query_two = new WP_Query(\'cat=3&amp;showposts=2\'); ?>
<?php while($query_two->have_posts()) : ?>
<?php
$postcount++;
if( ($postcount % 2) != 0 ) : // skip \'odd\' posts
    $wp_query->next_post();
else :
?>
<?php $query_two->the_post(); ?>

                    <?php if ( function_exists( \'get_the_image\' ) ) { get_the_image( array( \'custom_key\' => array( \'post_thumbnail\' ), \'default_size\' => \'full\', \'image_class\' => \'alignleft\', \'width\' => \'60\', \'height\' => \'60\' ) ); } ?>

<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // After every wp_query ?> 
</div> 
您可以在此处阅读更多信息:
http://codex.wordpress.org/Class_Reference/WP_Query

SO网友:petermolnar

Don\'t repeat yourself ;)

我认为应该这样做:

<?php
    query_posts( \'cat=3\' );
    $postcount = 0;
    while( have_posts() ) :
        the_post();
        $image = false;

        if ( function_exists( \'get_the_image\' ) ) :
            $image = get_the_image( array(
                \'custom_key\' => array(
                \'post_thumbnail\' ),
                \'default_size\' => \'full\',
                \'image_class\' => \'alignleft\',
                \'width\' => \'120\',
                \'height\' => \'120\',
                \'echo\' => false
            ) );
        endif;

        if( ($postcount % 2) == 0 )
            $images[\'even\'][] = $image;
        else
            $images[\'odd\'][] = $image;

        $postcount++;
    endwhile;

foreach ($images as $colname => $colimages )
{
    echo \'<div class="\' . $colname . \'-column">\';
    foreach ( $colimages as $img )
    {
        if ( $img != false )
            echo $img;
    }
    echo \'</div>\';
}

结束

相关推荐

Offset for Loop

我想为我的相关职位抵消4个职位。我正在使用Flowplayer的jQuery工具滚动,所以我希望人们能够滚动浏览最新的4篇帖子,然后是接下来的4篇帖子等等。我尝试了类似的方法,但不断收到错误消息:<h2>Related Posts</h2> <!-- \"previous page\" action --> <a class=\"prev browse left\"></a