Skip to main content

Display your WordPress Recent Posts FAQs

Rewind back 3 years when I published the tutorial display your WordPress Recent Posts on a Static Page.  I wouldn’t ever have dreamed that it would be one of the most commented posts on my blog and even now people are still asking for hints and tips on how to expand the code further so I’ve decided to go through all the comments and gathered enough to have a FAQ section.

WordPress Website Image
Image Provided by Gil C / Shutterstock.com

**Generally all code will be specified in the loop unless otherwise stated**

Featured Images (Post Thumbnails)

My featured Image doesn’t show up.

If your post thumbnails don’t work make sure you have set a featured image on your post edit screen and make sure you have included the following snippet in functions.php

<?php add_theme_support( 'post-thumbnails' );?>

Can I change the size of my featured image?

If you want to use the default image size then simply use

<?php the_post_thumbnail();?>

You can also change the size of your post’s featured image by choosing one of the following.

<?php the_post_thumbnail( 'medium' );?>
<?php the_post_thumbnail( 'large' );?>
<?php the_post_thumbnail( 'full' );?>
<?php the_post_thumbnail( array(100, 100) ); //Custom Size?>

If that doesn’t work try get_the_post_thumbnail

<?php echo get_the_post_thumbnail($post->ID, 'full'); ?>
<?php echo get_the_post_thumbnail($post->ID, array(100,100),'thumbnail');?>

Errors

If you get PHP errors check that your path to wp-blog-header is correct and you have you used a relative link

folder/wp-blog-header.php

rather than an absolute link

https://mydomain

Also check for any whitespace after the opening and closing PHP tags, particularly in your functions.php.

If you get 404 errors try adding the call to blog header below the doctype If that doesn’t work then add the following under the call to blog header but above the doctype tag.

<?php require('wp-blog-header.php');  

/*if you are getting 404 errors add the next 2 lines*/
 status_header(200);
 nocache_headers();

?>
<!--HTML starts here-->

To enable error messages but not display them check/add your wp-config.php for/with the following

<?php 

define('WP_DEBUG', true); // or false
if (WP_DEBUG) {
  define('WP_DEBUG_LOG', true);
  define('WP_DEBUG_DISPLAY', false);
  @ini_set('display_errors',0);
}  
?>

Pages & Posts

Can I get the full content or excerpt?

If you want to add the full content use

<?php the_content();?>

and if you only want to display a snippet use

<?php the_excerpt();?>

Can I display the full post content without the more tag?

Yes add the following above the loop

<?php global $more; $more=1; ?>

and the following  inside the loop

<?php the_content();?>

Can I display pages instead of posts?

Yes simply change an argument in the $args array.

$args = array( 'numberposts' => 6, 'post_status'=>"publish",'post_type'=>"page");//change post_type from post to page

Can I choose what posts or pages I want to display?

Yes but only by post or page ID

Can I choose what Categories I want to display?

Yes but only by Cat ID

Can I use a unordered list instead of a DIV?

Yes, sure you can just remember to add the opening

<ul>

tag above the foreach loop and the closing tag

</ul>

below the loop.

Can I use this code within my current WordPress theme?

Yes, but the code is slightly different depending on the outcome you want.  If you want the posts outside the loop then the original code snippet should work.

If you are adding the posts inside a theme page that already contains the loop then my preference would be to add a new function inside functions.php and call it within that loop, shown in the examples below.

The following goes inside functions.php

<?php

/*****
Get the latest 10 posts function to add to functions.php
*****/

function get_wow_posts(){

global $post;
$args = array( 
              'numberposts' => 6,
              'post_status'=>"publish",
              'post_type'=>"post",
              'orderby'=>"post_date");


$postslist = get_posts( $args );

echo '<ul id="latest_posts">';
 
foreach ($postslist as $post) :  setup_postdata($post); ?>
 
<li>
  <strong><?php the_date(); ?></strong><br />

  <a href="<?php the_permalink(); ?>" title="<?php the_title();?>"> 
<?php the_title(); ?>
 </a>

</li>
        <?php endforeach;?>
</ul>
    <?php wp_reset_postdata();
}
?>

Below is an example, highlighted on line 13, of displaying posts inside a typical theme page by calling the function

get_wow_posts()

above.  Please note that all themes differ.

<?php if ( have_posts() ) :?>

     <?php while ( have_posts() ) : the_post();?>

          <article <?php post_class('home-page'); ?>>

                 <?php if (!is_front_page()) : ?>
                      <h1 class='title'><?php the_title();?></h1>
                 <?php endif; ?>

                        <div class="content">

                                <?php get_wow_posts();?>

                        </div><!-- the-content -->

                    </article>

                <?php endwhile; ?>

            <?php else :?>
                <article class="post error">
                    <h1 class="404">Nothing has been posted like that yet</h1>
                </article>

<?php endif;?>

Can I use Custom Fields?

Yes, you can. For instance, if I created a custom field called ‘Teaser’ I would add the following.

<?php echo get_post_meta($post->ID, 'teaser', true);?>

How come I am only able to see 1 page or post?

Do you only have 1 post/page published? Publish more and see if it works.

Dates

How Can I display the date with each post?

Change

<?php the_date();?>

to

<?php echo get_the_date();?>

Server

Can I link to my WordPress from a different server?

No, try parsing the RSS feed as in the following tutorial.

Can I link to my WordPress site from another site on the same server?

Yes, you can but it depends on your server settings and would be a case of trial and error.

Does this work with wordpress.com websites?

No, you must have your own hosting.

Can I reduce the server load?

You can reduce the server load by disabling the theme when loading.

Add the following above the call to blog-header.

define('WP_USE_THEMES', false);

Possible Problems/ Debugging

Some image and post plugins may conflict with the snippet so if it doesn’t work try disabling them temporarily. If it doesn’t work for you try switching to the default WordPress theme that comes bundled with it.

Conclusion

Hopefully the above will answer any questions you may have. If it doesn’t then please feel free to post any questions and I’ll try my best to answer them and update this post periodically.

Topics:

15 responses to “Display your WordPress Recent Posts FAQs”

  1. Hi! I don’t know if you keep responding to this thread but I’ll give it a shot.
    We are making a website and our user wanted a news section. They wanted something simple so we offered wordpress for them.
    So we are trying to post the latest news on a static php web page but the functions the_permalink and the_title only return one value. When we print the array “$postslist” , it contains the full information, but when the for each starts, it fills 4 news (the amount specified in $args = array( ‘numberposts’ => 4, ‘post_status’=>”publish”,’post_type’=>”post”,’orderby’=>”post_date”);) but it’s always the same permalink and always the same name.
    Do you have any idea of why this might be?
    Thanks a lot.

  2. hi, thanks for the tutor, i have a problem…
    my WP installed at my subdomain. how can i call the ‘wp-blog-header.php’ from my main domain? thanks for your help…

    • Good question Nicko. I haven’t tried it, as I have never needed to but I would assume it’s the same as the subfolder scenario in the original tutorial. I’ll give you some examples but It all depends on how your website and your WP install is setup.

      If your website is in the root folder (www) and your WP sub-domain is in a folder (which is also in root) try:

       

      If your main website is also in a subfolder then try:

       

      The best thing to do, if you are not sure is to go into your server through Control Panel or FTP
      Find the file you want to add the snippet to
      Then follow the path to your wp install counting the steps (clicks) until you see wp-blog-header.
      The 1st example would be 1 step, the 2nd example 2 because you have to go back into the root directory to gain access to the WP Folder.

      You cannot directly link using an absolute URL like http://mydomain etc. so the path has to be relative

      Let me know how you get on and if you get any errors.

  3. Hi, thanks for the tutorial, didn’t try it yet but looks awesome. Do you know how to display last sticky notes only ?

  4. How do you customize the styles for the feed? I added “the_content” to my code to display the full post, however, any time I try to change the style (whether inserting it into a special div class or creating a #latest_posts id style), I can’t seem to override the style it’s retrieving, which seems to be my standard p class (and I’d prefer not to change this). I’m pretty green when it comes to php, so I’m unfamiliar with tricks for stylizing. This could be a CSS question instead of a PHP, but I thought the best place to ask is where I got the code to create the feed. Thanks in advance!

    • Hi Anna. If you are displaying the full content (the_content) then it would take on the style and formatting from the blog post itself, accessible from the WP Dashboard. It is a little different from (the_excerpt), which strips all formatting. You could add formatting and CSS classes to the blog post itself and reference them through the ID. If you are able to provide a URL I can easily take a look?

      • Thanks, Tracy, this is helpful. I’m currently working locally, but if I end up needing additional help, once I get out of designing/developing locally, I’ll make sure to reach back out for help with a URL. Thanks again. 🙂

      • Hi Tracy, I wanted to follow up and just let you know I figured it out. It just took a lot of trial and error with order of things in the CSS, understanding php a little bit better, and making a few changes in WordPress display options (i.e. the Read more message and Sep for September… small stuff). Thanks again for the post and offering to help!

  5. I thought showing blog posts on static homepage was going to take me hours of research – using your functions.php and shortcode above I got it done in about 15 minutes – thanks very much!

Have your say!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: