Run the Loop Outside of WordPress – using the Loop

How to run the loop of WordPress outside of WordPress in normal PHP Website or simple website.

I have created all the website completely on WordPress but sometime a developer needs half of WordPress functionalities in a custom website or CMS in PHP. If the client need a WordPress Blog but to list out their recent posts on custom Website. For this purpose You do not need to create a blog posting module and no query module. You need to just include a WordPress page either wp-blog-header.php or wp-load.php to fetch its functionalities. Enough talks lets learn How to Run the Loop Outside of WordPress.

Use WordPress Outside of WordPress

<?php
 // Include WordPress
      define('WP_USE_THEMES', false);
      require('/your_folder/wp-blog-header.php');
 ?>

define(‘WP_USE_THEMES’,false)
by means of this line we are restricting WordPress not to use default themes just need inner loops.

wp-blog-header.php
Include wp blog header file so we can use the post functionality outside of WordPress. This is the necessary file and it must exist all the time.

Run the Loop Outside of WordPress

<?php
query_posts('showposts=1');
     while (have_posts()): the_post(); ?>
          <h2><?php the_title(); ?></h2>
          <p>
             <?php the_excerpt(); ?>
             <a href="<?php the_permalink(); ?>" class="red">Read more...</a>
          </p>
     <?php endwhile; ?>

 

We are using query_posts which is listed here on WordPress codex of Query_posts. Use the loop to fetch posts and build your own recent posts blocks in static PHP website situated out of WordPress themes.


Moreover after including the wp header whole wealth and functionalities are on your hand (include the loops) to use WordPress functionalities in Custom Website. While we write example query to get one post. for more creativity purpose you need to write custom queries with WP_Query or get_posts().

Leave a Reply

Your email address will not be published. Required fields are marked *