Wordpress post loop

So im trying to add a function called "latest posts" in a sidebar section.

This is how my code looks like in sidebar.php-file.

  <?php

            query_posts('category = all');

    if (have_posts()) :

       while (have_posts()) : ?>

             <?php the_post();
          the_excerpt(); ?>


             <p class="datum2"><?php the_time('Y-m-d'); ?></p>
             <p class="Nyhetsrubrik3"><a style="orange"; href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>
                  <p class="textnyhet2">

       </p>


      <?php endwhile;
      endif;
    ?>

My problem is, no matter how much I move around in the code I just cant seems to get the correct title/date to appear above the correct post.

Now it's like the post comes first, then right under the post the date and title appears above another incorrect post etc.

Example:

Latest post text

space---->

title/date, (though to the post above)

latest post text

space----->

and continue like that.

Would be grateful if anyone could help me out.

Thanks!

Solutions

The reason the title/date appears below the post is because that's the way you have specified it to appear. Change

<?php the_post();
      the_excerpt(); ?>


         <p class="datum2"><?php the_time('Y-m-d'); ?></p>
         <p class="Nyhetsrubrik3"><a style="orange"; href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>
              <p class="textnyhet2">

   </p>

to

<?php the_post(); ?>

         <p class="datum2"><?php the_time('Y-m-d'); ?></p>
         <p class="Nyhetsrubrik3"><a style="orange"; href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>

     <?php the_excerpt(); ?>
              <p class="textnyhet2">

   </p>

It should display the way you want it.

the_excerpt(); is printing out an excerpt of the post, then the time/title displays. Now it should display the time and title first, then display the excerpt of the post.

Tags: PHP / Html / Wordpress

Similar questions

Wordpress loop : get current post count inside The Loop
When inside The Loop, I want to retrieve the current post count. For example, after every 3 posts, I want to insert an ad. So, how do I get the value of the loop count?
wordpress loop - style 1st post differently to the following (side loop get_posts())
In my wordpress theme, I want to add in the footer a side loop to fetch the latest posts. the first post of this loop displays thumb pix, title, and post preview.. the 5 following only displays the title/link. Since I already use the regular <?php if(have_posts()) : while(have_posts()) : the_post(); ?> in the main div, I have to use a side lo...
Nesting a Wordpress post loop inside a page loop
Making a one-page Wordpress theme. Using this loop to call all pages and display them as sections (html5 tags): This creates a nice little loop of sections based on pages. My only concern is: one of these "pages" / sections should show a few blog posts. Looking for a way to essentially create a loop inside a loop -- which can be reordered along wit...
In wordpress how to get post loop inside another loop
I want to get latest post based on author loop. this is my code: function display_artist_func() { $out_args = array( 'post_type' => 'product', 'author' => 1, 'showposts' => 3 ); $out_loop = new WP_Query($out_args); while ($out_loop->have_posts()) : ?> <div><?php echo get_avatar( get_the_author_email(), '80' ); ?&...
How to dynamically build a multiple taxonomy query loop within a post type's single loop?
This question is a little different then the others floating around here. The most similar one is "How to get the parent's taxonomy ?". I have a music cms setup that utilizes two post types: album and album media and a few taxonomies: genre, year, and artist. In the footer of every single-album.php post I want to display the corresponding album med...
display sticky post outside loop and exclude from loop
I have a "featured" area in header.php that displays the content of the latest sticky post: problem is that the same post now renders below it in the Loop - how can I suppress sticky posts from appearing at all in the loop?
PHP

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.