如何在同一循环中循环自定义帖子

时间:2015-08-25 18:14:18

标签: php wordpress

我是PHP的新手。所以请耐心等待。在WordPress主题中,我创建了一个自定义帖子类型(团队),现在我想在一个div中显示3个帖子标题,然后是3个帖子中每个帖子的内容(在不同的div中),然后重复对于每组3个帖子,这将持续到查询结束。

我的HTML看起来像这样,但我不知道如何组装PHP。所以任何帮助将不胜感激:

<div class="section-wrapper">
    <div class="member-row row-1">
        <div class="team-member member-1">
        <h2><?php the_title(); ?></h2>
        </div>
        <div class="team-member member-2">
        <h2><?php the_title(); ?></h2>
        </div>
        <div class="team-member member-3">
        <h2><?php the_title(); ?></h2>
        </div>
    </div>
    <div class="summary summary-1">
        <?php 
        // display the_content() of each of the 3 posts I just queried
        the_content(); 
        ?>
    </div>
</div>
<div class="section-wrapper">
    <div class="member-row row-2">
        <div class="team-member member-4">
        <h2><?php the_title(); ?></h2>
        </div>
        <div class="team-member member-5">
        <h2><?php the_title(); ?></h2>
        </div>
        <div class="team-member member-6">
        <h2><?php the_title(); ?></h2>
        </div>
    </div>
    <div class="summary summary-2">
        <?php 
        // display the_content() of each of the 3 posts I just queried
        the_content(); 
        ?>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

我会尝试更简单的HTML,但如果你想使用那个,你可以使用这个代码。

 <div class="section-wrapper">
<?php
// WP_Query arguments
$args = array (
    'post_type'              => 'team',
    // choose other differentiating parameter, for example tag 
    'tag'                     => 'main'
);

// The Query
$query = new WP_Query( $args );

// The loop
if( $query->have_posts() ): while( $query->have_posts()): $query->the_post(); 
?>
<div class="member-row row-1 <?php post_class(); ?>">
    <div class="team-member member-1">
    <h2><?php the_title(); ?></h2>
    </div>
</div>
<?php

endwhile; endif;

// Restore original Post Data
wp_reset_postdata();

?>

<?php
// WP_Query arguments
$args = array (
    'post_type'              => 'team',
    // choose other differentiating parameter, for example tag 
    'tag'                     => 'main'
);

// The Query
$query = new WP_Query( $args );

// The loop
if( $query->have_posts() ): while( $query->have_posts()): $query->the_post(); 
?>


<div class="summary <?php post_class() ?>">
    <?php 
    // display the_content() of each of the 3 posts I just queried
    the_content(); 
    ?>
</div>

<?php

endwhile; endif;

// Restore original Post Data
wp_reset_postdata();

?>
</div>


<div class="section-wrapper">
<?php
// WP_Query arguments
$args = array (
    'post_type'              => 'team',
    // choose other differentiating parameter, for example tag 
    'tag'                     => 'seconday'
);

// The Query
$query = new WP_Query( $args );

// The loop
if( $query->have_posts() ): while( $query->have_posts()): $query->the_post(); 
?>
<div class="member-row row-1 <?php post_class(); ?>">
    <div class="team-member member-1">
    <h2><?php the_title(); ?></h2>
    </div>
</div>
<?php

endwhile; endif;

// Restore original Post Data
wp_reset_postdata();

?>

<?php
// WP_Query arguments
$args = array (
    'post_type'              => 'team',
    // choose other differentiating parameter, for example tag 
    'tag'                     => 'secondary'
);

// The Query
$query = new WP_Query( $args );

// The loop
if( $query->have_posts() ): while( $query->have_posts()): $query->the_post(); 
?>


<div class="summary <?php post_class() ?>">
    <?php 
    // display the_content() of each of the 3 posts I just queried
    the_content(); 
    ?>
</div>

<?php

endwhile; endif;

// Restore original Post Data
wp_reset_postdata();

?>
</div>