【问题标题】:How to display latest posts in Wordpress如何在 Wordpress 中显示最新帖子
【发布时间】:2017-05-28 15:29:49
【问题描述】:

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

有没有一种简单的方法来检索 n 个最新的帖子以便显示它们?

【问题讨论】:

    标签: php css wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:
    <?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
    }
    ?>
    

    【讨论】:

      【解决方案2】:
      <?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 方面:[最新帖子]
      • 在文件中:&lt;?php echo do_shortcode('[lastest-post]'); ?&gt;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-24
        • 1970-01-01
        • 1970-01-01
        • 2016-10-23
        • 1970-01-01
        相关资源
        最近更新 更多