2017-01-13 106 views

回答

1
<?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文件。
  • 之后,粘贴shortcode下面你想显示最新的帖子。
  • 埃丁侧:最新的-交]
  • 在文件:<?php echo do_shortcode('[lastest-post]'); ?>
0
<?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 
} 
?> 
相关问题