作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在自定义我的 WordPress 主题,我想在我的 Wordpress 主页顶部添加最新的 2 个置顶贴。为此,我使用以下代码:
<div class="trending-right">
<?php
$sticky = get_option( 'sticky_posts' ); // Get all sticky posts
rsort( $sticky ); // Sort the stickies, latest first
$sticky = array_slice( $sticky, 0, 2 ); // Number of stickies to show
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); // The query
if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
<div class="trend-post">
<div class="thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
<div class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
</div>
<?php endwhile;?>
<?php } else { echo ""; }?>
</div>
现在代码工作正常并显示最新的 2 个置顶帖子,但它也从主页删除所有其他列出的帖子并仅显示这 2 个置顶帖子。我尝试将 query_posts
替换为 new WP_Query
但在那种情况下它会显示所有置顶帖子,而不是仅显示 2 个。
关于如何调整上面的代码并使其工作有什么建议吗?
最佳答案
看看你的代码,我假设你刚刚向我们展示了粘性循环,还有另一个查询来显示模板中其他地方的其他帖子?你应该使用 wp_reset_query();在您的自定义查询之后,这是 Codex 中的条目;
关于php - 在 WordPress 主页显示最新的 2 个置顶帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338924/
所以我需要能够在 wordpress 中发布特色或“粘性”帖子,我想到了!为什么不使用 Sticky Posts 工具,但在阅读了一些内容之后,似乎 Wordpress 决定在最新版本中不包含对它的支
我是一名优秀的程序员,十分优秀!