仅过滤 Wordpress 中的博客文章
Filter Only the Blog Posts in Wordpress
我想在页脚显示最新的 2 篇博文。我使用 while (have_posts()) : the_post();
在我的博客页面中显示所有博客文章(在我的例子中几乎是索引页面)。
当我尝试使用相同的方法过滤最新的博客文章时,我无法实现。
这是迄今为止我尝试过的代码。我使用 for loop
来限制帖子的数量。
<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>
Through this code I am only returned the Home
page link twice.
这里有什么问题?或者我可以尝试其他方法吗?
使用这个是为了显示2个帖子,你可以根据自己的方便更改ul li,
您可以使用 posts_per_page
选项过滤 2 个帖子
<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 篇博文。我使用 while (have_posts()) : the_post();
在我的博客页面中显示所有博客文章(在我的例子中几乎是索引页面)。
当我尝试使用相同的方法过滤最新的博客文章时,我无法实现。
这是迄今为止我尝试过的代码。我使用 for loop
来限制帖子的数量。
<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>
Through this code I am only returned the
Home
page link twice.
这里有什么问题?或者我可以尝试其他方法吗?
使用这个是为了显示2个帖子,你可以根据自己的方便更改ul li,
您可以使用 posts_per_page
选项过滤 2 个帖子
<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>