Skip to content

Instantly share code, notes, and snippets.

@karlazz
Last active June 14, 2021 01:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save karlazz/4638931 to your computer and use it in GitHub Desktop.
Save karlazz/4638931 to your computer and use it in GitHub Desktop.
Running WordPress functions as a standalone php file
<!DOCTYPE HTML>
<!-- attribution -- this blog provided the code
http://blog.makingsense.com/2011/03/create-stand-alone-web-pages-or-widgets-that-use-wordpress-functions/
-->
<?php
//required include files
require('wp-blog-header.php');
require_once("wp-config.php");
require_once("wp-includes/wp-db.php");
/* for wordpress standalone without database use
// Load WP components, no themes
define('WP_USE_THEMES', false);
require('wp-load.php');
*/
?>
<html>
<head>
<title>Stand alone page using wordpress functions</title>
</head>
<body>
<!-- our code goes here -->
<center><b>Recent Post</b></center>
<ul>
<?php
$posts = get_posts('numberposts=5&offset=0');
foreach($posts as $post) {
echo '<li><a href="' . get_permalink($post->ID) . '" title="'. $post->post_title .'" >' . $post->post_title .'</a> </li> ';
} ?>
</ul>
<div id="widget">
<div class="inside">
<center><b>Recent Wordpress Blog Posts</b></center>
<?php
$posts = get_posts('numberposts=5&offset=0');
$recent = "<ul style='margin-left: 0px'>";
foreach($posts as $post)
{
$ThumbnailImage = "";
$recent .= "\n";
$post_title = $post->post_title;
$guid = get_permalink($post->ID);
$ThumbnailID = get_post_thumbnail_id($post->ID);
if($ThumbnailID)
{
// get the image title
$ImageTitle = get_the_title( $ThumbnailID );
// get the url of the file
$thumbnail_file = wp_get_attachment_thumb_url($ThumbnailID);
// format the image and it’s link
$ThumbnailImage = '<a href="'.$guid.'" title="' . $post_title . '" target="_blank"><img height="35" width="35" class="widget-image thumbnail" src="' . $thumbnail_file . '" alt="'. $ImageTitle .'"/></a>';
}
// format the line with the image
$recent .= '<li>'. $ThumbnailImage . '<a href="'.$guid.'" target="_blank">' . $post_title.'</a><br><br><br></li>';
}
$recent .= "</ul>";
echo $recent;
?>
</div>
</div>
<!-- cool way to run it
<iframe src="<path to your blog>/my-blog-widget.php" width="100%" height="350px" style="margin-left:-8px;padding-left: 13px;" frameborder="0" scrolling="auto">
<p>Your browser does not support iframes.</p>
</iframe>
-->
</body>
</html>
@alana314
Copy link

+1 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment