create a PHP file for your blog called "blog.php" add the snippet below anywhere you want the html for your blog to be inserted. Keep in mind, the HTML will be totally un-styled so you may have to do a little CSS work to match your site's formatting and style.
<?php include("include/return_blog.php"); ?>
Next create a file called "return_blog.php" with the code below in it. Be sure to update the pertinent variables (read my comments in the code snippet). Put this file into a sub-folder called "include"
<?php
// Hackaday.io Blog Widget
// This code turns your project logs into a simple blog for your external website
// **** YOU MUST EDIT THE API AND PROJECT ID PARAMETERS BELOW TO REFLECT YOUR https://dev.hackaday.io/ API ACCOUNT ******
// enter your api key
$api_key = "XXXXXXXXXXXXXXXX";
// enter your project id, you can find it in the url of your project
$project_id = "XXXX";
// enter your blog url, for my blog it is http://www.stickvise.com/blog/
$blog_url = "http://xxxx";
// number of logs to display per page
$logs_per_page = 4;
// if $log_page is not set, default $log_page to 1
$log_page = ($_GET["log_page"] ? $_GET["log_page"] : 1);
// import json data from API
// get project object
$project_object = json_decode(file_get_contents("https://api.hackaday.io/v1/projects/".$project_id."?api_key=".$api_key),true);
// get array of project log objects (starting at $log_page, quantity limited by $logs_per_page)
$log_objects_array = json_decode(file_get_contents("https://api.hackaday.io/v1/projects/".$project_id."/logs?api_key=".$api_key."&sortby=newest&page=".$log_page."&per_page=".$logs_per_page),true);
// store the project url in a variable, this is the base for the individual log URLs
$project_url = $project_object['url'];
// store the total number of logs for this project in a variable
$project_numlogs = $project_object['logs'];
// loop through the log_objects_array and spit out HTML
foreach($log_objects_array['logs'] as $log) {
$log_title = $log['title'];
$log_body = $log['body'];
$log_id = $log['id'];
$log_date = $log['created'];
// get the main comment object for each log
$log_comments = json_decode(file_get_contents("https://api.hackaday.io/v1/comments/logs/".$log_id."?api_key=".$api_key),true);
// Print the log title and body
echo '
<h2><a href="'.$project_url.'/log/'.$log_id.'" target="_blank">'.$log_title.'</a><br/>
<small>'.date('M-d Y', $log_date).'</small>
</h2>
<p>
'.$log_body;
// Print log comments (if there are any for this log)
if ($log_comments['total']) {
echo '<h3>COMMENTS:</h3>';
print_comments($log_comments['comments']); // magic function
}
// Print the remainder of HTML
echo '
</p>
';
} // end loop through logs
// Print bottom nav
echo '
<h2>
';
if($log_page > 1) {
echo '
<a href="'.$blog_url.($log_page-1).'"><< Newer</a>
';
}
if ($log_page+1 <= $project_numlogs/$logs_per_page) {
echo '
<a href="'.$blog_url.($log_page+1).'">Older >></a>
';
}
echo '
</h2>
';
// print_comments function
function print_comments($comments) {
// loop through comments array
echo '<ul>';
foreach($comments as $comment_object1) {
// find and print first tier comments
if($comment_object1['reply_to'] == 0) {
echo '
<p><a href="'.$comment_object1['user']['url'].'" target="_blank">'.$comment_object1['user']['screen_name'].'</a><br />
'.$comment_object1['comment'].'
</p>';
// loop through comments array again to find second tier comments
echo '<ul>';
foreach($comments as $comment_object2) {
// find and print second tier comments replying to the above tier comment
if($comment_object2['reply_to'] == $comment_object1['id']) {
echo '
<p><a href="'.$comment_object2['user']['url'].'" target="_blank">'.$comment_object2['user']['screen_name'].'</a><br />
'.$comment_object2['comment'].'
</p>';
// loop through comments array third time to find third tier comments
echo '<ul>';
foreach($comments as $comment_object3) {
// find and print...
Read more »
There has been a great deal of value to me in my involvement with the project. Would like to share it with the https://gofinanc.com/ team so they can also read it and implement something new.