1

I am using a script I obtained from a Blog that displays 3 wordpress posts on a non-wordpress page as follows:

<div class="wrapper row">
    <?php
    define('WP_USE_THEMES', false);
    require('news/wp-load.php');
    query_posts('showposts=3');
    ?>

    <?php while (have_posts()): the_post(); ?>
        <div class="col-sm-4">
            <h2><?php the_title(); ?></h2>
            <p><em>Posted <?php the_date(); ?> by <?php the_author(); ?></em></p>
            <div class="image"><?php
                if ( has_post_thumbnail() )
                    the_post_thumbnail( 'thumbnail' );
                else
                    echo '<img src="news/wp-content/uploads/2015/06/150x150-2.jpg" alt="Example Image" title="Example" width="100px"/>';
                ?></div>
            <p><?php the_excerpt(); ?></p>
            <p><a href="<?php the_permalink(); ?>">Read more...</a></p>
        </div><!-- /.col-sm-4 -->
    <?php endwhile; ?>
</div>

I have currently got the loop ( 3 posts ) in a bootstrap grid repeating the 'col-sm-4' div across the 12 column grid.

What I would liked to do is have the first post in a 'col-sm-6' div and the other two in a 'col-sm-3' div.

Is there a way I can adapt this code to do this?

Thanks in advance!

3 Answers 3

1

You can have a simple $first = true flag which can then be used to output col-sm-6. Before the first run is finished you set the flag to false.

Here is your code rewritten with this in mind:

<div class="wrapper row">
<?php
define('WP_USE_THEMES', false);
require('news/wp-load.php');
query_posts('showposts=3');
?>

<?php $first = true; while (have_posts()): the_post(); ?>
<div class="<?php if ($first) { echo 'col-sm-6'; } else { echo 'col-sm-3'; } ?>">
<h2><?php the_title(); ?></h2>
<p><em>Posted <?php the_date(); ?> by <?php the_author(); ?></em></p>
<div class="image"><?php
if ( has_post_thumbnail() )
    the_post_thumbnail( 'thumbnail' );
else
     echo '<img src="news/wp-content/uploads/2015/06/150x150-2.jpg"   alt="Example Image" title="Example" width="100px"/>';
?></div>
<p><?php the_excerpt(); ?></p>
<p><a href="<?php the_permalink(); ?>">Read more...</a></p>
</div><!-- /.col-sm-4 -->
<?php $first = false; endwhile; ?>
</div>
5
  • Thanks for this Luka, is there a code typo error here as it is breaking the page. Dreamweaver picks out the following line with error. <div class="<?php if ($first) { echo 'col-sm-6' } else { echo 'col-sm-3' }"> Commented Nov 25, 2015 at 11:24
  • Steve, yes I had a typo. It should be OK now. Commented Nov 25, 2015 at 12:57
  • Luka, thanks for update. Unfortunately it still breaks the page. dreamweaver still doesn't like the same line of code <div class="<?php if ($first) { echo 'col-sm-6' } else { echo 'col-sm-3' } ?>">. Any ideas? Commented Nov 25, 2015 at 14:33
  • Steve, there was some semicolons missing. I've added them, tested this code and it runs now. Sorry for the inconvenience. Commented Nov 25, 2015 at 15:06
  • Luka, any idea how I can add to this by showing a list of permalinks ( <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>) but excluding the first three posts. Ideally this would appear in a fourth column in the same row as the 3 posts. So I would alter the divs to - 'col-sm-4' for post 1, 'col-sm-3' for post 2 and 3 and 'col-sm-2' for the permalinks excluding the first three posts. Commented Nov 25, 2015 at 15:58
1

For this you have to simply check if loop is running for first time i.e is it showing first post.

By simply initializing variable and make it increase by 1 every time while completes its loop.

I have made dummy code for it to make you understand clearly.

<?php 
$sn = 1; //initializing variable assigning 1 to it
while (have_posts()): the_post(); // your while loop
if($sn == 1){ // here we will check if $sn = 1 which means we are have first post if not then it might be 2nd or third
?>
    <div class="col-sm-6">
<?php } else { ?>
    <div class="col-sm-4">    
<?php } ?>        
        <h2><?php the_title(); ?></h2>
        <p><em>Posted <?php the_date(); ?> by <?php the_author(); ?></em></p>
        <div class="image">
            <?php
            if ( has_post_thumbnail() )
                the_post_thumbnail( 'thumbnail' );
            else
                echo '<img src="news/wp-content/uploads/2015/06/150x150-2.jpg" alt="Example Image" title="Example" width="100px"/>';
            ?>
        </div>
        <p><?php the_excerpt(); ?></p>
        <p><a href="<?php the_permalink(); ?>">Read more...</a></p>
    </div><!-- /.col-sm-4 -->
<?php 
$sn++;  // very important to increment variable by 1  so that we can check how many time loop has run
endwhile; ?>

I hope it will help you.

1
  • Thanks for your efforts Yatin, but this code is breaking the page. Can you see any code typos? Commented Nov 25, 2015 at 11:51
1
<div class="wrapper row">
    <?php
    define('WP_USE_THEMES', false);
    require('news/wp-load.php');
    query_posts('showposts=3');

    $isFirst = true;
    while (have_posts()): the_post();
    if($isFirst == true) {
        echo '<div class="col-sm-6">';
        $isFirst = false;
    } else {
        echo '<div class="col-sm-4">';
    }
    ?><h2><?php the_title(); ?></h2>
    <p><em>Posted <?php the_date(); ?> by <?php the_author(); ?></em></p>
    <div class="image"><?php
        if ( has_post_thumbnail() )
            the_post_thumbnail( 'thumbnail' );
        else
            echo '<img src="news/wp-content/uploads/2015/06/150x150-2.jpg" alt="Example Image" title="Example" width="100px"/>';
        ?></div>
        <p><?php the_excerpt(); ?></p>
        <p><a href="<?php the_permalink(); ?>">Read more...</a></p>
    </div><!-- /.col-sm-4 -->
<?php endwhile; ?>
</div>

Try this code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.