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

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

问题描述

在我的页面之一上,我希望有一个部分显示最新的3条新闻。

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

是否有一种简单的方法?

Is there a simple way of doing this?

推荐答案

<?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]'); ?>

    • Add Above code in function.php file.
    • After that paste below shortcode there you want to display latest post.
    • Adin side : [lastest-post]
    • in file : <?php echo do_shortcode('[lastest-post]'); ?>
    • 这篇关于如何显示最新帖子Wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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