{"id":32,"date":"2012-06-02T21:43:34","date_gmt":"2012-06-03T01:43:34","guid":{"rendered":"http:\/\/www.pat-matthews.com\/stories\/?p=32"},"modified":"2012-06-02T21:43:34","modified_gmt":"2012-06-03T01:43:34","slug":"wordpress-outside-of-wordpress","status":"publish","type":"post","link":"https:\/\/www.pat-matthews.com\/stories\/2012\/06\/02\/wordpress-outside-of-wordpress\/","title":{"rendered":"WordPress outside of WordPress"},"content":{"rendered":"<p>I do a fair amount of WordPress work, and one of the issues that frequently comes up is how to show WordPress posts outside of WordPress. In most cases, it&#8217;s easy enough to use wp-blog-header and the query_posts function.<!--more--><\/p>\n<p>That looks something like this:<\/p>\n<pre>&lt;?php\nrequire_once('pathtoblog\/wp-blog-header.php');\nquery_posts( array ( 'category_name' =&gt; $catname, 'posts_per_page' =&gt; 5 ) );\u00a0\u00a0\u00a0\necho '&lt;hr \/&gt;&lt;h2&gt;Recent Stories from the Blog about '.$catname.'&lt;\/h2&gt;';\u00a0\u00a0\u00a0\nwhile (have_posts()) :\n\u00a0\u00a0\u00a0\u00a0the_post();\n\u00a0 \u00a0 \/\/\u00a0use the_excerpt(),\u00a0 the_permalink(), and the_title() to display the post,\n\u00a0\u00a0\u00a0 \/\/ or the \"get_\" variants to populate strings with them.\nendwhile;\n?&gt;<\/pre>\n<p>Easy, right? What about when the blog is on a subdirectory or a different server, somewhere that &#8220;require_once&#8221; won&#8217;t reach? That&#8217;s when we have to go the RSS route:<\/p>\n<pre>&lt;?php\u00a0\u00a0\n\u00a0 $feed = simplexml_load_file('http:\/\/stories.daddytales.com\/?feed=rss2');\u00a0\n\u00a0 if ($feed) {\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0 $title=$feed-&gt;channel-&gt;item[0]-&gt;title;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0$link=$feed-&gt;channel-&gt;item[0]-&gt;link;\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0$description=$feed-&gt;channel-&gt;item[0]-&gt;description; \u00a0\u00a0\u00a0\n\u00a0 \u00a0$pubdate=getdate(strtotime($feed-&gt;channel-&gt;item[0]-&gt;pubDate));\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\n\u00a0\u00a0 unset($feed);\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0 $month=$pubdate['month'];\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\n\u00a0\u00a0 $day=$pubdate['mday'];\n\u00a0\u00a0 $year=$pubdate['year'];\u00a0\n\u00a0}\u00a0 else {\n\u00a0\u00a0 \/\/ do something because it couldn't get the feed.\n\u00a0}\n?&gt;<\/pre>\n<p>The above code only illustrates getting the text items for the first item of the list. To show the text items, use a foreach loop. Here&#8217;s an example:<\/p>\n<pre>&lt;?php\n $feed = simplexml_load_file('http:\/\/stories.daddytales.com\/?feed=rss2');\n if ($feed) {\u00a0\u00a0\n  echo '&lt;ul&gt;';\u00a0\u00a0 \u00a0\u00a0\n  foreach ($feed-&gt;channel-&gt;item as $feeditem) {\n   echo '&lt;li&gt;&lt;a href=\"'.$feeditem-&gt;link.'\"&gt;'.$feeditem-&gt;title.'&lt;\/a&gt;&lt;br \/&gt;'.$feeditem-&gt;description.'&lt;\/li&gt;';\n  } \/\/ end foreach loop\n  echo '&lt;\/ul&gt;';\n }\n?&gt;<\/pre>\n<p>As of this writing, you have to use the rss2 feed, because it&#8217;s the one that has a date, and I usually like to show the dates for posts.\u00a0The downside of this approach is that it&#8217;s hitting the server a second time, which is never good. But for cases where you can&#8217;t get to the wordpress files directly, it certainly does the trick! Also, if you don&#8217;t control the source of the feed, you might want to filter the text coming from it with an htmlentities or htmlspecialchar, or something like that.<\/p>\n<p>I hope this helps!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Need to display WordPress blog entries outside of WordPress? Here are a couple answers!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[70,81,94],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-techie","tag-php","tag-rss","tag-wordpress"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p6DpYi-w","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.pat-matthews.com\/stories\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pat-matthews.com\/stories\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pat-matthews.com\/stories\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pat-matthews.com\/stories\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pat-matthews.com\/stories\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":0,"href":"https:\/\/www.pat-matthews.com\/stories\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pat-matthews.com\/stories\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pat-matthews.com\/stories\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pat-matthews.com\/stories\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}