Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display links to WordPress blog posts on non-WP pages using the WordPress API.
//add this code at the end of "functions.php" (Theme Functions). This can be accessed through the WP admin menu under Appearance -> Editor -> Theme Functions
//this is the logic to get the posts you want
function displayHomePosts(){
//default values obj from post.php.
//pass these in as args that you really want
$defaults = array(
'numberposts' => 5, 'offset' => 0,
'category' => 0, 'orderby' => 'post_date',
'order' => 'DESC', 'include' => '',
'exclude' => '', 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post',
'suppress_filters' => true
);
$args = array(
'numberposts' => 4
);
//explore why the redirect error occurs?
//$num = 5;
$post_array = get_posts($args ); //as found in wp-includes/post.php
//var_dump($post_array);
//add the last className for the bottom border?
for($i=0; $i<count($post_array); $i++){
$post_title = $post_array[$i]->post_title;
$post_name = $post_array[$i]->post_name;
$post_permalink = "http://www.yourwebsite.com/blog/" . $post_name;
//actually output the html with the php variables included
echo "<li><a href=\"$post_permalink\" rel=\"bookmark\" title=\"Permanent Link to $post_title\">$post_title</a></li>";
}
}
<?php
//some of these settings are necessary for older WP versions. This was tested on 3.3.2 and works fine.
//define('WP_USE_THEMES', false); //don't style it like the WP stuff
//Loads the WordPress environment and template. This makes all the fn's available.
//point this at the location of the current WordPress installation.
require('blog/wp-blog-header.php');
//require_once('blog/wp-config.php'); //loads the db settings
//get_header();
?>
<h2 id="hThoughts"><a href="blog">Thoughts</a></h2>
<ul>
<?php displayHomePosts(); ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.