如何将所有wordpress帖子彼此分开?

时间:2011-11-16 14:38:12

标签: wordpress post

我正在使用Twentyeleven默认模板,我使用

    <?php if ( have_posts() ) : ?>
            <?php /* Start the Loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', get_post_format() ); ?>
            <?php endwhile; ?>
        <?php else : ?>
        <?php endif; ?>

获取帖子。

我想要的是:

<div id="wrapper">
<div id="first">
    //first post
    <?php get_template_part( 'content', get_post_format() ); ?>
</div>
<div id="second">
    //second post
    <?php get_template_part( 'content', get_post_format() ); ?>
</div>
<div id="third">
    //third post
    <?php get_template_part( 'content', get_post_format() ); ?>
</div>
</div><!-- end wrapper -->

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

你可以使用post-1而不是first,post-2而不是second,等等吗?如果是这样,死得很容易。创建一个变量,每次循环都会递增,并跟踪帖子的数量。

<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php $count = 0; /* <- create the counter variable */?>
    <?php while ( have_posts() ) : the_post(); ?>
        <div id="post-<?php echo $count; /* <- prints the variable */ $count++; /* <- increments the counter */ ?>">
            <?php get_template_part( 'content', get_post_format() ); ?>             
        </div>
    <?php endwhile; ?>         
<?php else : ?>         
<?php endif; ?>

从你的问题我不确定你是否意识到while和endwhile位之间的所有内容都插入到每个帖子的HTML中。这就是它被称为“循环”的原因。它循环遍历该部分,直到页面上的每个帖子都显示出来。

您可能还希望use the built-in post class function为每个帖子添加相关课程。以上是添加了post类功能的内容:

<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php $count = 0; /* <- create the counter variable */?>
    <?php while ( have_posts() ) : the_post(); ?>
        <div id="post-<?php echo $count; $count++;?>" <?php post_class(); ?>>
            <?php get_template_part( 'content', get_post_format() ); ?>             
        </div>
    <?php endwhile; ?>         
<?php else : ?>         
<?php endif; ?>