主页/WordPress笔记/博客文章/综合文章/如何在 WordPress 中显示最近的文章

如何在 WordPress 中显示最近的文章

Bug编译狮

Bug编译狮

在 WordPress 中显示最近的文章通常可以通过以下几种方式实现:

方法一:使用默认功能

WordPress 自带有显示最近文章的功能,只需要设置好相关选项即可。

步骤:

  1. 登录到您的 WordPress 管理后台。
  2. 导航到 Appearance > Customize(定制)。
  3. 在左侧菜单中选择 Writing
  4. 在页面顶部的搜索框中输入“最近文章”并点击搜索。
  5. 选择您想要显示的标签或类别来筛选文章。
  6. 按下保存更改按钮。

效果:现在,您的网站上会展示最近发布的文章列表。

示例代码

假设我们有一个简单的博客站点,我们需要展示最近发表的三篇文章。以下是相应的代码片段:

<?php
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 3,
);

$recent_posts = new WP_Query($args);
?>

<div class="recent-posts">
    <?php if ($recent_posts->have_posts()) : ?>
        <h2>最近的文章:</h2>
        <?php while ($recent_posts->have_posts()) : $recent_posts->the_post(); ?>
            <article>
                <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                    <div class="post-title">
                        <?php the_title(); ?>
                    </div>
                    <time datetime="<?php the_time('c') ?>"><?php the_time('F j, Y') ?></time>
                </a>
                <p><?php echo wp_trim_words(get_the_excerpt(), 50); ?></p>
            </article>
        <?php endwhile; ?>
        <ul class="pagination">
            <?php previous_posts_link('<i class="fa fa-angle-left"></i>'); ?>
            <?php next_posts_link('<i class="fa fa-angle-right"></i>'); ?>
        </ul>
    <?php endif; ?>
</div>

<?php wp_reset_postdata(); ?>

这段代码首先定义了一个数组 $args 来限制查询为所有类型的帖子(即所有类型的文章),然后创建一个新的 WP_Query 对象。当有符合条件的文章时,它会循环显示这些文章的信息,包括标题、发布时间和摘要。

方法二:自定义插件

如果需要更复杂的功能,可以考虑安装一些自定义插件,例如 Recent Posts Plus 或者 Post List Shortcode,它们允许您自定义显示文章的方式和样式。

注意事项

  • SEO:确保您的文章内容对搜索引擎友好,以便提高排名。
  • 用户体验:避免过度显示最近文章,以免影响其他重要信息的可见性。

通过以上方法,您可以轻松地在 WordPress 网站上展示最近发布的文章。

黑板前的Bug语法讲师

黑板前的Bug语法讲师

在 WordPress 中显示最近的文章是一项基本操作,通过以下步骤可以轻松实现:

  1. 首先,在 WordPress 的主题文件夹下找到 functions.php 文件。

  2. 打开 functions.php 文件,查找并替换其中的以下行:

    add_action( 'wp_head', 'my_recent_posts' );
  3. functions.php 文件中添加一个名为 my_recent_posts 的函数,如下所示:

    function my_recent_posts() {
       $recent_posts = get_posts('numberposts=5&orderby=date&order=desc');
       foreach($recent_posts as $post) : setup_postdata($post);
           echo '<a href="' . esc_url(get_permalink()) . '">' . get_the_title() . '</a>';
       endforeach;
    }
  4. 然后,在您的 WordPress 主题的 <body> 标签内,插入以下代码以启用该功能:

    <?php wp_head(); ?>
    <style>
       .recent-post { margin: 0; padding: 0; list-style-type: none; font-size: 18px; text-align: center; }
       .recent-post a { color: #666; text-decoration: none; }
       .recent-post a:hover { text-decoration: underline; }
    </style>
    <script type="text/javascript">
       jQuery(document).ready(function($) {
           $( ".recent-post" ).click(function() {
               window.location.href=$(this).attr("href");
           });
       });
    </script>
    
    <div class="recent-post">
       <?php my_recent_posts(); ?>
    </div>
  5. 运行您的网站并查看结果。您应该看到最近发布的文章链接列表。

以上代码将从 WordPress 的数据库中获取前五篇文章(根据发布时间排序),然后显示这些文章的标题和链接。您可以根据需要调整这篇文章数和排序方式。

请确保在运行此代码之前备份您的网站,以防万一出现问题。