On my Bond page, I have installed WordPress as a blogging tool in the folder nullnullsju.net/blogg. But I wanted to display the last blogposts on the main frontpage. So how to display WordPress post outside the main blog folder?
To display the post on other pages, you need to first load WordPress’ wp-load.php file into the page in question. And the PHP code is as follow:
<?php
define('WP_USE_THEMES', false);
require('./blogg/wp-load.php');
query_posts('showposts=2');
?>
To explain the lines. The first tells WordPress it shouldn’t use WordPress’ theme. The second imports the file. You obviously need to change this to reflect your installation. And the last line tells WordPress how many posts to display. In this case, it shows two posts.
If you want to display the posts on another site, change the require line to include the whole URL.
So now you have loaded the posts, and now you have to display them. And you can do that by a code similar to this:
<?php while (have_posts()): the_post(); ?> <h1><?php the_title(); ?></h1> <a href="<?php the_permalink(); ?>"> <?php the_excerpt(); ?> </a> <?php endwhile; ?>
You can of course format this as you want. To explain the code:
The first line starts the loop that displays the posts.
The second line displays the post title (in H1 tags).
The third line starts the link with the post’s permalink.
The forth line prints the excerpt (who then becomes the link). The link is closed in the fifth line, and the loop is closed in the last line.
If you want to display the content of the post, you use the_content() in the code.
