Surely it has happened to you that you have several pages on the same server and you would like to show the content of your site that is WordPress, on one of the other sites or even see products from your online store in WooCommerce on another of your sites, you can even create a application and drink from our favorite CMS such as WordPress.
<?php define('WP_USE_THEMES', false); require('./wp-blog-header.php'); ?>
Then; How to use WordPress outside of WordPress in real life?
Generate a list of posts:
The official documentation also says that; in case you want to display for example; ten posts arranged alphabetically in ascending order on your web page, you can do the following to get the published date, title, and excerpt:
<?php require('/the/path/to/your/wp-blog-header.php'); ?> <?php $posts = get_posts('numberposts=10&order=ASC&orderby=post_title'); foreach ($posts as $post) : setup_postdata( $post ); ?> <?php the_date(); echo "<br />"; ?> <?php the_title(); ?> <?php the_excerpt(); ?> <?php endforeach; ?>
Or this other example too: And a catch just in case…
<?php // Get the last 3 posts. global $post; $args = array( 'posts_per_page' => 3 ); $myposts = get_posts( $args ); foreach( $myposts as $post ) : setup_postdata($post); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br /> <?php endforeach; ?>
The code shown above must be placed in a .php file, Outside the folder where WordPress is installed, which is located inside a folder (at the same level as WordPress), in the root of the site. Remember that the following must be taken into account:
The WordPress functionality call code is used in the header of the file.
We use the WordPress global variable $post.
The other part of the code retrieves recent entries through a loop (which we assume you already know how to do).
You already know how to use WordPress outside of WordPress
Finally, you have to keep in mind that although WordPress is fast, it contains a large amount of code that must be loaded every time a page is displayed. This may or may not affect performance depending on the hosting environment, but in a shared hosting environment using SuPhp (and thus without opcode caching) it can add several seconds to each page load.