循环浏览最近的3个帖子,但将它们划分为单独的div

时间:2014-05-26 作者:wharfdale

我试图循环浏览3篇文章,但将每一篇文章分为自己的div,因为一些jQuery会一次显示和隐藏一篇文章。

我已经创建了代码来显示一个,但我不确定是否将它们分开。

<div id="news-container-1">
    <?php
    // Create a variable to hold our custom Loop results
    $excludes  = array(\'4135\');
    $frontpageposts = get_posts( array( 
         \'numberposts\' => 1, // only the 3 latest posts
         \'post__not_in\' => $excludes
    ) );

    // Create output only if we have results
    // Customize to suit your HTML markup
    if ( $frontpageposts ) { 

         foreach ( $frontpageposts as $fppost ) { 
              // setup postdata, so we can use template tags
              setup_postdata($fppost);
              ?>

              <div <?php post_class(); ?>>
                   <h4><a href="<?php the_permalink(); ?>"><?php //the_title(); ?>Latest News #1</a></h4>
                   <div class="post-entry">
                        <?php                                                
                                $content = $post->post_content;
                                $searchimages = \'~<img [^>]* />~\';

                                /*Run preg_match_all to grab all the images and save the results in $pics*/

                                preg_match_all( $searchimages, $content, $pics );

                                // Check to see if we have at least 1 image
                                $iNumberOfPics = count($pics[0]);

                                if ( $iNumberOfPics > 0 ) { ?>
                                    <!-- GRAB THE IMAGE AND USE WHATEVER HTML YOU LIKE -->
                                      <img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /></a>
                                <?php }
                        ?>
                        <?php //the_excerpt(); ?>
                   </div>
              </div>

    <?php }
    } 
    ?>
</div>
在本例中,我已将“numberposts”更改为1,因为此处显示的3将显示第一个news-container-1 div中的所有3。

其他两个部门是:

<div id="news-container-2"></div>
<div id="news-container-3"></div>

1 个回复
最合适的回答,由SO网友:anu 整理而成

这并不是WordPress的问题,但您只需为所需的类整数设置一个计数器,如下所示:

    <?php
    // Create a variable to hold our custom Loop results
    $excludes  = array(\'4135\');
    $frontpageposts = get_posts( array( 
         \'numberposts\' => 3, // only the 3 latest posts
         \'post__not_in\' => $excludes
    ) );

    // Create output only if we have results
    // Customize to suit your HTML markup
    if ( $frontpageposts ) { 

         $i = 1;

         foreach ( $frontpageposts as $fppost ) { 
              // setup postdata, so we can use template tags
              setup_postdata($fppost);
              ?>

             <div id="news-container-<?php echo $i++ ?>">

              <div <?php post_class(); ?>>
                   <h4><a href="<?php the_permalink(); ?>"><?php //the_title(); ?>Latest News #1</a></h4>
                   <div class="post-entry">
                        <?php                                                
                                $content = $post->post_content;
                                $searchimages = \'~<img [^>]* />~\';

                                /*Run preg_match_all to grab all the images and save the results in $pics*/

                                preg_match_all( $searchimages, $content, $pics );

                                // Check to see if we have at least 1 image
                                $iNumberOfPics = count($pics[0]);

                                if ( $iNumberOfPics > 0 ) { ?>
                                    <!-- GRAB THE IMAGE AND USE WHATEVER HTML YOU LIKE -->
                                      <img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /></a>
                                <?php }
                        ?>
                        <?php //the_excerpt(); ?>
                   </div>
              </div>
    </div>


    <?php }
    } 
    ?>

结束

相关推荐

通过函数在帖子下方插入WordPress标签。php

我正在从头开始构建自己的Wordpress主题,而我对PHP的知识有限。我想做的是在每篇文章下面插入标签。我可以通过在单曲中插入代码来实现这一点。php文件,但我想通过functions.php 文件我已经通过函数在每篇帖子下面插入了作者框。php文件。我在网上的某个地方得到了它的代码,我想对标签做同样的事情。这是我的functions.php 文件function get_author_bio($content=\'\') { global $post; $post_au