0

This is code of a category.php file, I use the bootstrap 12 column grid. The posts are given out in 6 column rows. But I want a picture to be next to the first post. I tried to accomplish this with a do while statement, I am just getting started with php and I couldn't figure it out, console tells me:

unexpected T_INLINE_HTML, expecting T_WHILE on the Line with "XXX"

    <?php $x = 1 ?>

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<div class="row">
    <div class="col-md-6" style="margin-bottom: 50px">
        <h3><?php the_title(); ?></h3>
            <?php the_content() ;?>
    </div>

    <?php do ?>
        <div class="col-md-6" style="float: right">
XXX     <img src ="<?php echo get_stylesheet_directory_uri(); ?>/images/wolf.png">
        </div>
    <?php $x++; ?>
    <?php while ($x <= 1); ?>
</div>

<?php endwhile; ?>
<?php endif; ?>

This didn't work for me, how do I add this correctly?

        <?php if ( $wp_query->current_post == 0 ) ?>
        <div class="col-md-6" style="float: right">
        <img src ="<?php echo get_stylesheet_directory_uri(); ?>/images/wolf.png">
        </div>
8
  • This is not your first post, please work on your formatting. Add code in a code block by indenting each line with 4 spaces or by using the {} sign above the editor when you create a post. Others are not there to clean up your mess. Thank you for understanding :-) May 18 '15 at 6:50
  • Also, your code makes no sense at all. Create one loop and one loop only. You have plenty May 18 '15 at 6:51
  • Please leave errors in blockquotes or code blocks May 18 '15 at 6:54
  • Sorry Pieter, I was immediately reediting my post when I saw the code was not shown properly, if you would have waited 1 minute I would have cleaned it myself already, thanks anyway. I am new to php, I don't see why I shouldn't put loops inside loops, from a theoretical standpoint it made sense to me.
    – MrKainig
    May 18 '15 at 6:54
  • What does This didn't work for me mean. Are you getting errors, a blank page, what?. BTW, please recheck my answer and use the code exactly as I have given it. You cannot just remove the braces and expect the code to work. By removing the braces, the condition only targets the first line after it, not any line after the first. This is basic PHP May 18 '15 at 7:43
0

The error you are getting is quite specific and tells you exactly what is wrong. Also debug errors are off topic here.

Your real issue here is adding an image to the first post only, which you can accomplish with the build-in loop counter $current_post that starts at 0, so the first post will be 0, not 1.

Inside your loop, you can do the following

if  ( $wp_query->current_post == 0 ) {
    // Add your picture for first post only here
}

EDIT

Your code in your edit should be:

<?php if ( $wp_query->current_post == 0 ) { ?>

    <div class="col-md-6" style="float: right">
        <img src ="<?php echo get_stylesheet_directory_uri(); ?>/images/wolf.png">
    </div>
<?php } ?>
7
  • Actually I didn't express myself clearly enough, I want the picture to be next to the top post which isn't the first but rather the most current. But thanks for the tip, definitely going to use that in future!
    – MrKainig
    May 18 '15 at 7:19
  • Please reread your question, that is not what you've asked for. Please don't waste other's time by posting incorrect info. Please file an edit and add the correct info inside your question May 18 '15 at 7:26
  • How do I implement your code correctly anyway? And Yes I just figured out that I have to tackle the whole thing from the start because I didn't think about the first post in my case probably is not going to be the one at the top. So your answer would be correct to my question and this case is solved and can be closed
    – MrKainig
    May 18 '15 at 7:28
  • I don't understand, in normal logic, the first post will always be the newest. By default, posts in the main query is ordered by date, the newest post being on top and the oldest at the bottom. This behavior can only be changed by one of the relevant filters where you change the sorting order. May 18 '15 at 7:34
  • Well posts are used for all kinds of stuff right? In my case for example to make a track list, it wouldn't make no sense to put the newest to the buttom. Your little trick is not working for me yet and if I put the code the way you showed me the <?php is missing.
    – MrKainig
    May 18 '15 at 7:43

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.