如何在 WordPress 中显示一个类别中的第二个和第三个最近的帖子 [英] How do I display the second and third most recent posts from a category in WordPress

查看:27
本文介绍了如何在 WordPress 中显示一个类别中的第二个和第三个最近的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的主页上显示最近三篇新闻文章的预览.最近的帖子将以与第二个和第三个最近的帖子不同的格式显示.我目前使用以下代码显示所有三个相同

<?php query_posts('cat=2 && showposts=3');if (have_posts()) : while (have_posts()) : the_post();?><div class="col-xs-12 col-sm-4"><div class="column"><div class="news-article"><p class="news-date"><?php the_time(get_option('date_format'));?></p><a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img'));?><p class="news-headline"><?php the_title();?></p></a><p><?php the_excerpt();?></p><a href="<?php the_permalink(); ?>"><p class="pull-right">阅读更多...</p></a><span class="clearfix"></span>

<?php终了;万一;?>

如何添加另一个循环,将第二个和第三个最近的帖子与最近的帖子分开?我不想使用 postID,因为帖子会改变.

解决方案

未经测试,但您可以尝试:

 <div class="col-xs-12 col-sm-4"><div class="column"><div class="news-article"><p class="news-date"><?php the_time(get_option('date_format'));?></p><a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img'));?><p class="news-headline"><?php the_title();?></p></a><p><?php the_excerpt();?></p><a href="<?php the_permalink(); ?>"><p class="pull-right">阅读更多...</p></a><span class="clearfix"></span>

<?php$count = 1;}别的{//这里还有一些其他的布局}终了;万一;?>

上面将检查 $count 是否为 0 ,如果是,则进行布局,然后计数将等于 1.所以下一次 $count 不会是 0,所以它会运行 else(这将是你的布局)中的内容.

I am displaying previews of the three most recent news articles on my homepage. The most recent post will be displayed in a different format to the second and third most recent posts. I am currently displaying all three the same with the following code

<?php query_posts('cat=2 && showposts=3'); 
                    if (have_posts()) : while (have_posts()) : the_post(); ?>
                        <div class="col-xs-12 col-sm-4">
                            <div class="column">
                                <div class="news-article">
                                    <p class="news-date"><?php the_time( get_option( 'date_format' ) ); ?></p>
                                    <a href="<?php the_permalink(); ?>">
                                        <?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img' )); } ?>
                                        <p class="news-headline"><?php the_title(); ?></p>
                                    </a>
                                    <p><?php the_excerpt(); ?></p>
                                    <a href="<?php the_permalink(); ?>">
                                        <p class="pull-right">Read more...</p>
                                    </a>
                                    <span class="clearfix"></span>
                                </div>
                            </div>
                        </div>
                    <?php
                    endwhile;
                    endif;
                    ?>

How can I add another loop which will separate the second and third most recent posts from the most recent post? I did not want to use postID as the posts will change.

解决方案

Untested but you could try:

    <?php 
    $count = 0;
    query_posts('cat=2 && showposts=3'); 
    if (have_posts()) : while (have_posts()) : the_post(); 
    if($count == 0)
    {
    ?>
        <div class="col-xs-12 col-sm-4">
            <div class="column">
                <div class="news-article">
                    <p class="news-date"><?php the_time( get_option( 'date_format' ) ); ?></p>
                    <a href="<?php the_permalink(); ?>">
                    <?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img' )); } ?>
                    <p class="news-headline"><?php the_title(); ?></p>
                     </a>
                     <p><?php the_excerpt(); ?></p>
                    <a href="<?php the_permalink(); ?>">
                    <p class="pull-right">Read more...</p>
                    </a>
                    <span class="clearfix"></span>
                </div>
            </div>
        </div>
    <?php
    $count = 1;
    }
    else
    {
        //Some other layout here
    }
    endwhile;
    endif;
    ?>

The above will check if $count is 0 and if it is, then do the layout and the count will then equal to 1. So the next time around $count won't be 0, so it will run what is in the else (which will be your layout).

这篇关于如何在 WordPress 中显示一个类别中的第二个和第三个最近的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆