如何在 Wordpress 中显示最新帖子 [英] How to display latest posts in Wordpress

查看:34
本文介绍了如何在 Wordpress 中显示最新帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个页面上,我想要一个部分来显示最新的 3 个新闻帖子.

On one of my pages, I want a section to show the latest 3 news posts.

是否有一种简单的方法可以检索 n 个最新帖子,以便显示它们?

Is there a simple way to retrieve the n latest posts so that they can be displayed?

推荐答案

<?php
function latest_post() {

    $args = array(
        'posts_per_page' => 3, /* how many post you need to display */
        'offset' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post', /* your post type name */
        'post_status' => 'publish'
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) :
        while ($query->have_posts()) : $query->the_post();
            ?>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php echo get_the_post_thumbnail('thumbnail'); ?>
            /* here add code what you need to display like above title, image and more */
            <?php
        endwhile;
    endif;
}

add_shortcode('lastest-post', 'latest_post');
?>

  • 在function.php文件中添加以上代码.
  • 粘贴到下面的短代码之后,你想显示最新的帖子.
  • Adin 方面:[lastest-post]
  • 在文件中:<?php echo do_shortcode('[lastest-post]');?>
  • 这篇关于如何在 Wordpress 中显示最新帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆