简体   繁体   中英

Loop through sticky posts in wordpress?

I'm new to Wordpress and I'm trying to loop through sticky posts with this code:

<?php
      /* Get all sticky posts */
      $sticky = get_option( 'sticky_posts' );

      /* Sort the stickies with the newest ones at the top */
      rsort( $sticky );


      /* Query sticky posts */
      $stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
      ?>

      <?php
       foreach ($stickies as $sticky) {

       the_title();
       comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' );


       }

      ?>

However in the out put if I have 2 stickies posts, the first one is displayed two times...

Any idea ?

Thanks a lot for your help !

EDIT :

It seems that

foreach ($stickies['WP_Post Object'] as $sticky) { 

get the good two articles, but I still have this error message : Warning: Invalid argument supplied for foreach()...

Full code :

 <?php
  /* Get all sticky posts */
  $sticky = get_option( 'sticky_posts' );
  /* Sort the stickies with the newest ones at the top */
  rsort( $sticky );
  $stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
  ?>

  <?php
   foreach ($stickies['WP_Post Object'] as $sticky) {

   if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
     the_post_thumbnail('thumbnail', array('class'  => "media-object img-rounded"));
   } 
   the_title();
   comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' );


   }

  ?>

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

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

  <div class="media">
    <a class="pull-left" href="<?php the_permalink() ?>">
    <?php 
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
      the_post_thumbnail('thumbnail', array('class' => "media-object img-rounded"));
    } 
    ?>
    </a>
    <div class="media-body">
      <h3 class="media-heading"><a class="permalink" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <?php the_title(); ?></a></h3>
      Par <?php the_author(); ?>, le <?php the_time('j F Y'); ?> - <?php comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' ); ?>
      <?php the_excerpt(); ?>
    </div>
  </div> <!-- /media -->



  <?php endwhile; ?>


  <?php endif; ?>

Try this:

$stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1, 'ignore_sticky_posts' => true) );

EDIT:

I think you should use standard wordpress posts loop:

while (have_posts()) : the_post();
    the_title();
endwhile;

EDIT:

You can use rewind_posts() to start a new loop:

// main loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>

// rewind
<?php rewind_posts(); ?>

// new loop
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM