【问题标题】:WordPress removing post from Recent PostWordPress 从最近的帖子中删除帖子
【发布时间】:2015-01-15 02:04:31
【问题描述】:

我希望我的代码能够删除第一个查询中显示的帖子,并在上面显示的帖子之后显示 3 个最近的帖子。

热门帖子:

<?php query_posts('posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post();?> 
<h2><?php the_title(); ?></h2>
<div class="news-feat-img">
<a href="<?php the_permalink(); ?>" ><?php the_post_thumbnail('indexsavedimage'); ?></a>
</div>
<?php the_excerpt(); ?> 
<?php endwhile; endif; wp_reset_query();?>

最近的帖子:

</a><div class="clearboth"></div>
<div class="borderline"></div>
<?php query_posts('posts_per_page=3'); if (have_posts()) : while (have_posts()) : the_post();?> 
<!-- Older news articles -->
<div class="news-posts">
<div class="news-thumb-wrap">
<div class="news-thumb">
<a href="<?php the_permalink(); ?>" ><?php the_post_thumbnail('newsimages'); ?></a>
</div>
</div>
<!-- Truncate long titles to stop the layout from messing up! -->
<a class="title" href="<?php the_permalink(); ?>">
<strong><?php the_title(); ?></strong>
</a>
<div class="clearboth"></div>
<?php the_excerpt(); ?> 
<div class="news-action">
<span class="label"><?php the_category( ', ' ); ?></span>
<a href="<?php the_permalink(); ?>" ><span class="label"><i class="icon-share icon-white"></i>    </span></a>
</div>
</div>
<?php endwhile; endif; wp_reset_query();?>

【问题讨论】:

  • 那么,您想获取第一个热门帖子 (3) 并以特定样式显示第一个帖子,然后再以不同样式显示其他 2 个帖子?抱歉,看错了。
  • @phatskat 您看到顶部非常大,并且也显示在底部吗?我想从底部删除该帖子,并在该帖子之后显示三个最近的帖子。

标签: php wordpress


【解决方案1】:

首先,如果您还没有阅读 WP_Query,请阅读并准备好您的论据 - http://codex.wordpress.org/Class_Reference/WP_Query

一个重要的论点是posts_per_page

<?php
$posts = WP_Query(array(
    'posts_per_page' => 4,
    # ...
));

# From WordPress's example, modified of course
$i = 0;
if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
                $the_query->the_post();

                if(!$i)
                {
                    // First style
                    echo "Some HTML for the first post,<br/>";
                }
                else
                {
                    // Code for second style
                    echo "HTML for the rest!";
                }

                $i++;
        }
} else {
        // no posts found
}

编辑:抱歉,误读了?哈哈。给你

【讨论】:

  • 完美,正是我想要的。
猜你喜欢
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 1970-01-01
  • 2015-11-17
  • 1970-01-01
相关资源
最近更新 更多