Display wordpress posts on non wordpress page
Many times we need all wordpress functions outside the wordpress. For example if we need to create API for send the data to users in json format or XML. Then we need to create sample.php file on root. I share two useful examples for get the wordpress functions outside the wordpress. With follow these examples you can do any type of work of wordpress in outside file.
Method 1:
wp-load.php is the wordpress core file which included all the functions of wordpress. This is light weight method. but it doesn’t call wp()
or invoke the template loaders used by themes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Include the wp-load.php require './wp-load.php'; // Get the recent posts $getrecent_posts = wp_get_recent_posts(array( 'numberposts' => -1 )); foreach($getrecent_posts as $post) { echo '<a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a>'; } |
Method 2:
if you need all the WordPress the default functions, hooks/actions and theme functions. You can use wp-blog-header.php file in simple php file.
1 2 3 4 5 6 7 8 |
define('WP_USE_THEMES', true); /** Loads the all WordPress hooks/actions and Template functions */ require ('./wp-blog-header.php'); |
If you are facing any problem in WordPress functionality or customisation contact me feel free or comment below this is my pleasure if I can help you. Thanks for reading.