首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在同一个循环中遍历自定义帖子

如何在同一个循环中遍历自定义帖子
EN

Stack Overflow用户
提问于 2015-08-26 02:14:18
回答 1查看 178关注 0票数 1

我是PHP新手。所以请耐心听我说。在WordPress主题中,我已经创建了一个自定义帖子类型(team),现在我希望在一个div中显示3个帖子标题,然后显示3个帖子中每个帖子的内容(在不同的div中),然后对每组3个帖子重复此操作,直到查询结束。

我的HTML看起来像这样,但是我不知道如何组装PHP。因此,如果有任何帮助,我们将不胜感激:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2015-08-29 07:33:45

我会尝试更简单的html,但是如果你想使用它,你可以使用下面的代码。

代码语言:javascript
复制
 <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32211192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档