簡體   English   中英

如何在 Wordpress 中顯示最新帖子

[英]How to display latest posts in Wordpress

在我的一個頁面上,我想要一個部分來顯示最新的 3 個新聞帖子。

有沒有一種簡單的方法來檢索n 個最新帖子,以便它們可以顯示?

<?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 文件中添加以上代碼。
  • 在粘貼下面的短代碼之后,你想顯示最新的帖子。
  • 阿丁方面:[最新帖子]
  • 在文件中: <?php echo do_shortcode('[lastest-post]'); ?> <?php echo do_shortcode('[lastest-post]'); ?>
<?php
//Query 3 recent published post in descending order
$args = array( 'numberposts' => '3', 'order' => 'DESC','post_status' => 'publish' );
$recent_posts = wp_get_recent_posts( $args );
//Now lets do something with these posts
foreach( $recent_posts as $recent )
{
    echo 'Post ID: '.$recent["ID"];
    echo 'Post URL: '.get_permalink($recent["ID"]);
    echo 'Post Title: '.$recent["post_title"];
    //Do whatever else you please with this WordPress post
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM