1

I use this script to display related posts:

<!-- 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 -->

The script displays html like this:

<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>

How do I get my related posts script to display html like this:

<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 Answer 1

0

Rewrite your code like this condition. This will display the code as you expected.

    <!-- 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 -->
6
  • Hi @Gopalakrishnan18 This code does not work. This makes my page blank.
    – R.M. Reza
    Commented Dec 3, 2019 at 6:15
  • Now try it. I made a silly mistake sorry for that. Commented Dec 3, 2019 at 6:16
  • This time it worked @Gopalakrishnan18 , but the html layout was not what was expected. screenshoot: link
    – R.M. Reza
    Commented Dec 3, 2019 at 6:24
  • Now try it, i have changed the code's condition @Renomu Reza Commented Dec 3, 2019 at 6:32
  • Well this works and is as expected. Thank you very much @Gopalakrishnan18 I really appreciate.
    – R.M. Reza
    Commented Dec 3, 2019 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.