简体   繁体   中英

PHP Wordpress - Call latest 3 posts

I'm quite a novice with PHP, but would like some assistance with how to display the latest 3 posts at the bottom of a page in wordpress. I want the title of each post displayed, along with the image belonging to the post and maybe the first line of text in the post, with a read more button, that I can then style with CSS to match the rest of the site.

Is this easy to achieve?

Thank you for any help.

Try this:

$args = array(
    'numberposts'     => 3,
    'offset'          => 0,
    'category'        => ,
    'orderby'         => 'post_date',
    'order'           => 'DESC',
    'include'         => ,
    'exclude'         => ,
    'meta_key'        => ,
    'meta_value'      => ,
    'post_type'       => 'post',
    'post_mime_type'  => ,
    'post_parent'     => ,
    'post_status'     => 'publish' );

get_posts($args);

Wordpress reference

Retrieving the posts can be done with get_posts() :

 <?php $posts_array = get_posts( array('numberposts' => 3) ); ?> 

In terms of rendering the posts, it would be worth checking whether your theme (or an installed plugin) provides some kind of "post preview" widget. If it does, save some heartache and use it. If it doesn't, you can print the posts yourself along these lines:

<?php foreach ($posts_array as $post): setup_postdata($post); ?>

  <!-- template tag: print thumbnail -->
  <?php get_the_post_thumbnail($post->ID); ?>

  <!-- template tag: print title -->
  <h3><?php the_title(); ?></h3>

  <!-- template tag: print excerpt -->
  <p><?php the_excerpt(); ?></p>

<?php endforeach; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM