0

I'm using a customized wordpress loop which allows me to style the post a certain way after a certain amount of post. For example, The first three post will be styled the same, the next 4 post will styled differently from the first three and so forth. It also allows me to add <div> sections between the post.

Here is the code php code I am using to accomplish this.

if (have_posts()) :
    $count = 0; $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
    while (have_posts()) : the_post();
        $count++;
        if ($count <= 3 && $paged === 1) :
            if ($count === 1) echo '<div class="break"><h2>Break div | first 3 posts</h2></div>'; ?>


            <div class="first-three">
                <?php the_title() ?>
            </div>
        <?php elseif (3 < $count && $count <= 7 && $paged === 1) :
            if ($count === 4) echo '<div class="break"><h2>Break div | next 4 posts</h2></div>'; ?>


            <div class="next-four">
                <?php the_title() ?>
            </div>
        <?php elseif (7 < $count && $count <= 13 && $paged === 1) :
            if ($count === 8) echo '<div class="break"><h2>Break div | next 6 posts</h2></div>'; ?>


            <div class="next-six">
                <?php the_title() ?>
            </div>
        <?php elseif (13 < $count && $count <= 20 && $paged === 1) :
            if ($count === 14) echo '<div class="break"><h2>Break div | next other 6 posts</h2></div>'; ?>


            <div class="next-other-six">
                <?php the_title() ?>
            </div>
        <?php else :
            if ($count === 21) echo '<div class="break"><h2>Break div | last 6 posts</h2></div>'; ?>

            <div class="last-six">
                <?php the_title() ?>
            </div>
        <?php endif;
    endwhile; ?>
    <div class="nav-previous alignleft"><?php next_posts_link('Older posts'); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link('Newer posts'); ?></div><?php
endif;

I'm not sure if you needed all of that, but I decided to place it there just in case.

Now here is the part I am having complications with.

    <?php elseif (3 < $count && $count <= 7 && $paged === 1) :
        if ($count === 4) echo '<div style="clear:both;"></div> <div class="large-6 columns" style="max-width:566px;  float:left;"> ', include 'thumbnail-break.php', '</div> '; ?>

For this part I added the include function to place a template path between the post. It returns the path but it also outputs the number 1 after the content.

How do I get rid of the number 1.

0

You should use instead of include('thumbnail-break.php');:

 get_template_part('thumbnail','break');

Which is the standard WordPress way of including partial template files from within page templates.

0

That's because you're trying to echo it out while it returns 1 on success.

Do this instead:

if ($count === 4) echo '<div style="clear:both;"></div> <div class="large-6 columns" style="max-width:566px;  float:left;"> '; include 'thumbnail-break.php'; echo '</div> '; ?>
  • That didn't work, it kind of messed up my html – Terrell Anderson Jun 3 '16 at 1:42
  • It should, but how it messed it up? – Samuel Elh Jun 3 '16 at 1:58
  • it included the include file 4 times and all other <div> elements became unwrapped @samuel ELH – Terrell Anderson Jun 3 '16 at 2:09
  • Please share with us your code using pastebin.com – Samuel Elh Jun 3 '16 at 2:13
0

According to the documentation on php.net you will find this line:

Handling Returns: include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1.

Try switching the following:

include 'thumbnail-break.php', '</div> '; ?>

To:

include_once 'thumbnail-break.php', '</div> '; ?>

The include_once only returns a true statement, which I don't believe will render in your html. Untested, but let me know how it goes. At the very least you now know why the 1 is emitted.

  • 1
    include_once is a bad practice here as I think it will get that file to be included in the first time only. Just a thought. – Samuel Elh Jun 3 '16 at 2:05
  • adding the ; before the '</div>'; ?> doesn't work @Nebri – Terrell Anderson Jun 3 '16 at 2:10

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.