在相关帖子中循环WordPress

时间:2019-12-03 作者:R.M. Reza

我使用此脚本显示相关帖子:

<!-- Related Posts START-->
<?php 
    $query = get_max_related_posts();
    if ( $query ) {
?>
<div class="td_block_wrap td_block_7 td_block_widget td_uid_3_5de39f94df644_rand td-pb-border-top td_block_template_1">
    <h4 class="block-title">
        <span class="td-pulldown-size">Berita Terkait</span>
    </h4>
    <div class="td_block_inner">
        <div class="td-block-row">
            <?php while ( $query->have_posts() ) { $query->the_post(); ?>
            <div class="td-block-span6">
                <div class="td_module_6 td_module_wrap td-animation-stack">
                    <div class="td-module-thumb">
                        <a href="<?php the_permalink(); ?>" rel="bookmark" class="td-image-wrap" title="<?php the_title(); ?>">
                            <?php the_post_thumbnail(array(100,75) ); ?>
                        </a>
                    </div>
                    <div class="item-details">
                        <h3 class="entry-title td-module-title">
                            <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>" ><?php the_title(); ?></a>
                        </h3>
                    </div>
                </div>
            </div>
            <?php } wp_reset_postdata();  ?>
        </div>
    </div>
</div>
<?php } ?>
<!-- Related Posts END -->
脚本显示如下html:

<div class="td_block_inner">
  <div class="td-block-row">
    <div class="td-block-span6">...</div>
    <div class="td-block-span6">...</div>
    <div class="td-block-span6">...</div>
    <div class="td-block-span6">...</div>
    <div class="td-block-span6">...</div>
    <div class="td-block-span6">...</div>
  </div>
</div>
如何让我的相关帖子脚本显示如下html:

<div class="td_block_inner">
  <div class="td-block-row">
    <div class="td-block-span6">...</div>
    <div class="td-block-span6">...</div>
  </div>
  <div class="td-block-row">
    <div class="td-block-span6">...</div>
    <div class="td-block-span6">...</div>
  </div>
  <div class="td-block-row">
    <div class="td-block-span6">...</div>
    <div class="td-block-span6">...</div>
  </div>
</div>

1 个回复
最合适的回答,由SO网友:Gopala krishnan 整理而成

像这样重写代码。这将按预期显示代码。

    <!-- Related Posts START-->
<?php 
    $query = get_max_related_posts();
    if ( $query ) {
    $i=1;
?>
<div class="td_block_wrap td_block_7 td_block_widget td_uid_3_5de39f94df644_rand td-pb-border-top td_block_template_1">
    <h4 class="block-title">
        <span class="td-pulldown-size">Berita Terkait</span>
    </h4>
    <div class="td_block_inner">
        <?php while ( $query->have_posts() ) { $query->the_post(); 
            if($i%2!=0){?>
                <div class="td-block-row"><?php
            }?>
            <div class="td-block-span6">
                <div class="td_module_6 td_module_wrap td-animation-stack">
                    <div class="td-module-thumb">
                        <a href="<?php the_permalink(); ?>" rel="bookmark" class="td-image-wrap" title="<?php the_title(); ?>">
                            <?php the_post_thumbnail(array(100,75) ); ?>
                        </a>
                    </div>
                    <div class="item-details">
                        <h3 class="entry-title td-module-title">
                            <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>" ><?php the_title(); ?></a>
                        </h3>
                    </div>
                </div>
            </div><?php
            if($i%2==0){ ?>
                </div><?php
            }
            $i++;
        } wp_reset_postdata();  ?>
    </div>
</div>
<?php } ?>
<!-- Related Posts END -->

相关推荐

Modals using loops and ACF

我试图制作一个页面,查询特定类别(“景点”)的每一篇帖子。我已经能够成功地获得帖子,我只需要让modals工作。我在我的循环中做了一个按钮,它的标题是循环所在的任何帖子。我希望这样,每当人们单击该按钮时,它就会打开一个模式,显示代码中ACF I列表中的所有字段。不过,我有一些问题。由于某种原因,我无法让javascript正常工作。现在都在页面模板文件中,但我已经尝试通过函数将脚本排队。php等。我的猜测是,我正在尝试制作文档。getElementsByClassName而不是documents。getE