【问题标题】:Wordpress Posts Page - loop is not showing latest postWordpress 帖子页面 - 循环未显示最新帖子
【发布时间】:2019-08-24 22:51:48
【问题描述】:

我对php比较陌生。

我的 Wordpress 帖子页面有一个循环 - 帖子必须在左右对齐之间交替。

我通过为每个帖子分配偶数或奇数类来实现这一点,但是现在最新的帖子不会显示在帖子页面上。

例如,如果我说 5 个帖子;其中 4 个帖子将显示,最新帖子将一直隐藏,直到我发布新帖子 - 之前隐藏的帖子将加入其他帖子,新的“最新帖子”将保持隐藏。

我不知道为什么我的循环会跳过第一个帖子,我已经尝试添加 rewind_posts();然而,这造成了同一篇文章的无限循环。

非常感谢任何帮助!

<?php 
$postcount=1;
while(have_posts()) :        
    if( ($postcount % 2) == 0 ) $post_class = ' even';
    else $post_class = ' odd'; 

    ?>

 <div class="row">
 <div id="stories-box-alt" class="stories-column-circle-main" 
 style="background-color:transparent;">
 <div id="circle-shape" class="post <?php echo $post_class; ?>">            
 <?php the_post(); ?>


 <img src="<?php the_field('post_preview_image'); ?>" class="curve">    

 <h2><?php the_title(); ?></h2>

 <h3><span class="featured-title"><?php the_field('post_category'); ?> . 
 </span></h3>

 <p><?php the_field('post_preview'); ?><br><a href="<?php the_permalink(); 
 ?>">read more...</a></p>

 </div>
 </div>           
 </div>
 <?php $postcount++;
        endwhile; ?>

【问题讨论】:

    标签: php wordpress loops posts


    【解决方案1】:
    <?php 
    $postcount=1;
    while(have_posts()) :        
    
        ?>
    
     <div class="row">
     <div id="stories-box-alt" class="stories-column-circle-main" 
     style="background-color:transparent;">
     <div id="circle-shape" class="post <?php if(($postcount % 2) == 0){  ?> even <?php } else{ echo " odd"; }?>">            
     <?php the_post(); ?>
    
    
     <img src="<?php the_field('post_preview_image'); ?>" class="curve">    
    
     <h2><?php the_title(); ?></h2>
    
     <h3><span class="featured-title"><?php the_field('post_category'); ?> . 
     </span></h3>
    
     <p><?php the_field('post_preview'); ?><br><a href="<?php the_permalink(); 
     ?>">read more...</a></p>
    
     </div>
     </div>           
     </div>
     <?php $postcount++;
            endwhile; ?>
    

    <?php echo $postcount % 2 == 0 ? ' even ': ' odd '; ?>
    

    【讨论】:

    • 对不起,我错过了;在回声“奇数”的末尾;
    【解决方案2】:

    请先尝试使用the_post()

    <?php 
        $postcount=1;
        while(have_posts()) :  
        the_post();      
            if( ($postcount % 2) == 0 ) $post_class = ' even';
            else $post_class = ' odd'; 
    
            ?>
    
         <div class="row">
         <div id="stories-box-alt" class="stories-column-circle-main" 
         style="background-color:transparent;">
         <div id="circle-shape" class="post <?php echo $post_class; ?>">            
    
    
         <img src="<?php the_field('post_preview_image'); ?>" class="curve">    
    
         <h2><?php the_title(); ?></h2>
    
         <h3><span class="featured-title"><?php the_field('post_category'); ?> . 
         </span></h3>
    
         <p><?php the_field('post_preview'); ?><br><a href="<?php the_permalink(); 
         ?>">read more...</a></p>
    
         </div>
         </div>           
         </div>
         <?php $postcount++;
                endwhile; ?>
    

    【讨论】:

    • 很遗憾仍然错过了第一篇文章,不过还是谢谢
    • 您可以尝试在循环之前添加 rewind_posts()。只需放置 rewind_posts();就在之前 (have_posts())..
    • 请尝试使用 一段时间后;我希望它有效。 :)
    【解决方案3】:

    wordpress 中基本上有一个基本循环来完成你想做的事情:https://wpchannel.com/wordpress/tutoriels-wordpress/afficher-articles-recents-site-wordpress/

    你可以用你自己的属性来修改这个,但这通常是循环使用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多