dp19001 2013-10-20 17:23
浏览 39
已采纳

循环通过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; ?>
  • 写回答

1条回答 默认 最新

  • doukuang1950 2013-10-20 17:27
    关注

    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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示