What Is the WordPress PHP Command for Get Recent Posts?
WordPress lets you set up a website quickly using pre-made themes, post and page templates that upload to the site. The website platform is open source, so a particular theme you have may not feature some of the functionality you need. Inside WordPress, you can update your site’s theme posts to include recent posts, exactly where you want to put them.
-
WordPress Site Construction
-
WordPress uses PHP, an open source language designed specifically for website development that works well with HTML. CSS defines the way the website looks, while PHP uses scripting language to execute commands. The marriage of the two creates the website. With the WordPress Dashboard, you can edit various pages and posts to add commands to the website or change its functionality. If the theme you use doesn’t include a “Get Recent Posts” command, insert it into your main index or post template, then place it where you need it.
Get Recent Posts Command
-
The “Get Recent Posts” command is located in the “posts.php” file in the directory of your website. It follows this protocol: “http://www.Websitename.com/wp-includes/post.php.” The command may already exist, with the “wp_get_recent_posts()” file name. Use the search function to find “get_recent_posts()" to look for it in the "post.php" file. The WordPress codex changes as newer versions of WordPress are released, so verify that this command will work with your website. The theme you use may be based upon an earlier version of WordPress and create conflicts. Edit the file from inside WordPress. Access "Appearance" from the Dashboard, then select "Editor." Scroll down the right side of the page until you find "post.php" and select it. The coding appears in the editing screen. You can also save it to your hard drive and edit it using a text editor.
-
Get Recent Post Syntax
-
To add the 10 most recent posts to your template, find an adequate place to enter this coding on the main or post template, available somewhere near the top:
<h2>Recent Posts</h2>
<ul>
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.$recent["post_title"].'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul>Insert this into your main or post template on the editing page where you want it to appear. Before you do that, make a backup of the existing main or post template and save it to your hard drive, so that you can replace it if something happens or you enter the language in the wrong place. If this doesn't work, change the "wp_get_recent_posts();" to "get_posts();."
Change the Number of Posts
-
If you want to change the number of posts that appear from 10 to five, change the syntax:
<h2>Recent Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.$recent["post_title"].'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul>Change the number "5" in “$args = array( ‘numberposts’ => ‘5’);” to the number of posts you want to appear.
-