首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >只过滤Wordpress中的博客文章

只过滤Wordpress中的博客文章
EN

Stack Overflow用户
提问于 2018-09-11 06:12:42
回答 1查看 50关注 0票数 1

我想在我的页脚上显示最新的2篇博客文章。我使用while (have_posts()) : the_post();来显示我的博客页面中的所有博客文章(几乎是我的例子中的索引页面)。

当我试图用同样的方法过滤最新的博客文章时,我无法做到这一点。

这是到目前为止我尝试过的代码。我使用for loop来限制帖子的数量。

代码语言:javascript
运行
复制
<ul class="widget-post-list">
     <?php if (have_posts()) : while (have_posts()) : the_post(); for ($i = 0; $i <2; $i ++){ ?>

     <li>
       <h5 class="post-title"><a href="<?php echo get_permalink() ?>"><?php echo wp_trim_words(get_the_title(), 14); ?></a></h5>
     </li>

     <?php } endwhile; else: ?>
       <h5>No Posts found.</h5>
     <?php endif; ?>
</ul>

通过这段代码,我只返回了两次Home页面链接。

这里有什么问题?或者还有别的办法我可以试试吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-11 06:22:52

使用这个来显示两个帖子,您可以根据您的方便更改ul li,

您可以使用posts_per_page选项筛选2篇文章

代码语言:javascript
运行
复制
<ul class="widget-post-list">
// Define our WP Query Parameters
<?php $the_query = new WP_Query( 'posts_per_page=2' ); ?>

// Start our WP Query
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

// Display the Post Title with Hyperlink
<li><h5 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h5></li>

// Repeat the process and reset once it hits the limit
<?php 
endwhile;
wp_reset_postdata();
?>
</ul>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52269693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档