WordPress循环中的Custon内容

时间:2018-07-27 作者:Milan Bastola

我创建了一个WP循环,以显示来自“服务”自定义帖子类型的服务:

Here is the code:

<?php
    $services_loop = new WP_Query( array( \'post_type\' => \'service\', \'posts_per_page\' => -1 ) );
    if ( $services_loop->have_posts() ) :
        while ( $services_loop->have_posts() ) : $services_loop->the_post(); ?>
          <section class="single-service">
            <div class="container">
                <div class="row">
                    <div class="col-md-6">
                        <?php the_post_thumbnail(\'full\', array(\'class\' => \'img-fluid w-100\', \'alt\' => get_the_title(), \'title\' => get_the_title())); ?>
                    </div>
                    <div class="col-md-6">
                        <h3><?php the_title(); ?></h3>
                        <?php the_content(); ?></div>
                    </div>
                </div>
            </div>
        </section>
        <?php endwhile;
    endif;
    wp_reset_postdata();
?>
它显示服务列表。但我想要的是,我想在循环的中间有一个自定义部分,我没有找到任何解决方案。在这里,我将想象我的问题。

enter image description here

2 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

您可以使用current_post 字段WP_Query 检查循环内部,也可以将循环分为两个循环。以下是代码示例:

<?php
    $services_loop = new WP_Query( array( \'post_type\' => \'service\', \'posts_per_page\' => -1 ) );
    while ( $services_loop->have_posts() ) : $services_loop->the_post();
?>
    <section class="single-service">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <?php the_post_thumbnail(\'full\', array(\'class\' => \'img-fluid w-100\', \'alt\' => get_the_title(), \'title\' => get_the_title())); ?>
                </div>
                <div class="col-md-6">
                    <h3><?php the_title(); ?></h3>
                    <?php the_content(); ?></div>
                </div>
            </div>
        </div>
    </section>
    <?php if ( 1 == $services_looop->current_post ) : ?>
    <div>
        Custom Content
    </div>
    <?php endif; ?>
<?php
    endwhile;
    wp_reset_postdata();
?>
或者将循环拆分为两个循环的解决方案:

<?php
    $services_loop = new WP_Query( array( \'post_type\' => \'service\', \'posts_per_page\' => -1 ) );
    while ( $services_loop->have_posts() && $services_loop->current_post < 2 ) :
        $services_loop->the_post();
?>
    <section class="single-service">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <?php the_post_thumbnail(\'full\', array(\'class\' => \'img-fluid w-100\', \'alt\' => get_the_title(), \'title\' => get_the_title())); ?>
                </div>
                <div class="col-md-6">
                    <h3><?php the_title(); ?></h3>
                    <?php the_content(); ?></div>
                </div>
            </div>
        </div>
    </section>
<?php endwhile; ?>
<div>
    Custom Content
</div>
<?php while ( $services_loop->have_posts() ) : $services_loop->the_post(); ?>
    <section class="single-service">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <?php the_post_thumbnail(\'full\', array(\'class\' => \'img-fluid w-100\', \'alt\' => get_the_title(), \'title\' => get_the_title())); ?>
                </div>
                <div class="col-md-6">
                    <h3><?php the_title(); ?></h3>
                    <?php the_content(); ?></div>
                </div>
            </div>
        </div>
    </section>
<?php
    endwhile;
    wp_reset_postdata();
?>

SO网友:Andrea Somovigo
<?php
$services_loop = new WP_Query( array( \'post_type\' => \'service\', \'posts_per_page\' => -1 ) );
$index=0;
if ( $services_loop->have_posts() ) :
    while ( $services_loop->have_posts() ) : $services_loop->the_post(); ?>
      <section class="single-service">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <?php the_post_thumbnail(\'full\', array(\'class\' => \'img-fluid w-100\', \'alt\' => get_the_title(), \'title\' => get_the_title())); ?>
                </div>
                <div class="col-md-6">
                    <h3><?php the_title(); ?></h3>
                    <?php the_content(); ?></div>
                </div>
            </div>
        </div>
    </section>
    <?php 
    if($index==3){ //this will output your content after the 4th post of the loop
    ?>
      <div class="inner_content">your content</div>
    <?php
    }
    $index++
  endwhile;
endif;
wp_reset_query();
?>
结束

相关推荐

如何链接到详细信息页面(single.php?)在wp_loop中

我进行了如下wp\\U查询。这是其中的一部分。这是可行的。$args = .....; $query = new WP_Query($args); while($query->have_posts() ) : $query->the_post(); ?> <div class=\"each\"> <h4><?php the_title(); ?> &