0

I currently trying to insert a block of custom code inside the WordPress posts loop which is using JetPack's infinite scroll functionality. In theory, this should be quite simple but is proving otherwise.

this is what I have currently written:

    <?php
        $count = 0;
        while( have_posts() ) :
            the_post();

            get_template_part( 'content-post', get_post_format() );
            if ($count === 4) {
                get_template_part( 'block-one', get_post_format() );
            }

            $count ++;
        endwhile;
    ?>

So, if the count is at 4 insert the custom block. However, when I look at my homepage, I get this view:

enter image description here

The custom code block has been added before the posts. I can't quite find out why.

Any suggestions?

EDIT:

Here is the content.php:

<a class="post-container-home" href="<?php the_permalink() ?>">
    <div
        class="post-image"
        style="background: url(<?php the_post_thumbnail_url() ?>)"
    >
    </div>
    <div class="post-info-home">
        <h3>Test</h3>
        <p>Title</p>
        <div class="post-time">
            <h3>min read</h3>
        </div>
    </div>
</a>

Here is block-one.php:

<div id="newsletter" class="post-container-home">
    <img src="/theme-new/wp-content/themes/theme-blog-new/img/subscribe.png" alt="subscribe">
</div>

Thanks,

  • Hard to tell without seeing the individual template parts, but it's likely that the block-one part is echoing or directly outputing, while the content-post is returning. – WebElaine May 12 at 17:28
  • @WebElaine Added those two templates to the post. – Charlie Coplestone May 12 at 18:21
0

I'm a fool.

I just noticed I'm trying to use content-post when I changed it to just content.

All working now.

Sorry.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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