0

This is my modified Wordpress loop w/ PHP If and Else Condition code.

    <?php if (have_posts()) : ?>

<?php
$i = 1;

while (have_posts()) {

    the_post(); 


    if ($i <= 1) {

            echo '<div class="firstp">';

                echo '<a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';
                    the_post_thumbnail('article-oneimg', array( 'id'    => "article-oneimg")); 
                echo '</a>';

                echo '<div class="thedate1">';
                    the_date();
                echo '</div>';

                echo '<h1><a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';;
                    the_title();
                echo '</a></h1>';

                echo '<p>';
                    the_excerpt();
                echo '</p>';

            echo '</div>';



    } elseif ($i <= 10) {
        echo '<div class="HOLDER-OF-secondp">';     
        include "secondp.php";
        echo '</div>';



    } else {
        // There should be a bunch of HTML mixed in here too
        the_title();
    }
    $i++;
}

?>



<?php else : ?>

<h2>No Posts Found</h2>

<?php endif; ?>

secondp.php contains this code:

<div class="secondp">
    <a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>">
        <?php the_post_thumbnail('article-twoimg', array( 'id'  => "article-twoimg")); ?>
    </a>
    <div class="thedate2">
        <?php the_date(); ?>
    </div>
    <h1>
        <a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h1>
</div>

What I'm trying to do here is just to add a holder/container of the secondp.php since it has several posts. But every time I use an echo '<div class="HOLDER-OF-secondp">'; before & after the include "second.php"; this happens (see image below taken using Firebug.)

enter image description here

What's wrong with the HTML structure inside the PHP IF condition? I trying to achieve is to put a container that holds all the divs class secondp.

In simple HTML, it's something like this:

<div class="holderofsecondp">
<div class="secondp">
a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>">
        <?php the_post_thumbnail('article-twoimg', array( 'id'  => "article-twoimg")); ?>
    </a>
    <div class="thedate2">
        <?php the_date(); ?>
    </div>
    <h1>
        <a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </h1>
</div>
</div>
6
  • Is HOLDER-OF-secondp div code in loop ? Can you post the complete code of that file ? Commented Apr 10, 2014 at 10:00
  • you have a problem with the condition if($i == 1) { echo '<div class="firstp">'; echo '<p>'; the_excerpt(); echo '</p>'; echo '</div>'; } } elseif ($i <= 10 && $i >1) { echo '<div class="HOLDER-OF-secondp">'; include "secondp.php"; echo '</div>'; Commented Apr 10, 2014 at 10:01
  • let me know what you want exactly ?? may be your $i is incrementing . Commented Apr 10, 2014 at 10:02
  • Hi Ron, i've posted my modified WP loop. you can also refer to this stackoverflow.com/questions/22963786/multiple-wordpress-loops for your reference on the purpose of that loop. Commented Apr 10, 2014 at 10:05
  • yea I cot you but let me know waht eaxctly you want ,I will write down the code depending upon that . Do you not need "HOLDER-OF-secondp" this appear multiple time . Just tell me the structure you want Commented Apr 10, 2014 at 10:11

3 Answers 3

1

Try this code:-

<?php global $wp_query;?>
<?php if (have_posts()) : ?>

<?php
$i = 1;
$start_hoder = true
$total_posts = $wp_query->found_posts;
while (have_posts())
{

    the_post();

    if ($i <= 1) {

        echo '<div class="firstp">';

            echo '<a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';
                the_post_thumbnail('article-oneimg', array( 'id'    => "article-oneimg"));
            echo '</a>';

            echo '<div class="thedate1">';
                the_date();
            echo '</div>';

            echo '<h1><a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';;
                the_title();
            echo '</a></h1>';

            echo '<p>';
                the_excerpt();
            echo '</p>';

        echo '</div>';

    } elseif ($i <= 10) {
        if($start_hoder)
        {
            echo '<div class="HOLDER-OF-secondp">';
            $start_hoder = false;
        }
            include "secondp.php";
        if($total_posts == $i && $start_hoder == false)
        {
            echo '</div>';
        }
    } else {
        // There should be a bunch of HTML mixed in here too
        the_title();
    }
    $i++;
}
?>

<?php else : ?>

<h2>No Posts Found</h2>

<?php endif; ?>

May be this will help you to get your expected output.

Sign up to request clarification or add additional context in comments.

3 Comments

there's an error on this $total_posts = $wp_query->found_posts;
you can also try global $wp_query; $total_posts = $wp_query->post_count;.
see accepted answer for count here:-wordpress.stackexchange.com/questions/78833/…
-1

Looks like you have a loop, an inside your loop you are repeatedly echoing the holder. You want to rearrange your code to not have the holder inside the loop.

1 Comment

I know, that's really my problem. I don't know how.
-1
<div class="holderofsecondp">
<?php  while (have_posts()) { ?>

    <div class="secondp"></div>
    <div class="secondp"></div>
    <div class="secondp"></div>
    <div class="secondp"></div>
    <div class="secondp"></div>
    <div class="secondp"></div>
    <div class="secondp"></div>

<?php } ?>

</div>

<div class="holderofsecondp"> this will be always out of the loop

or in your code Do this

<?php if($i==0) {?> <div class="HOLDER-OF-secondp"><?php } ?>

and add the div out of loop but its not good idea . Let me know if you need more help .

1 Comment

If someone downgrading answers please put your comments bcos its not a competition and we are here to help :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.