Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How can I show latest posts outside of forum?

edited October 2011 in Vanilla 2.0 - 2.8
I want to promote my forum on my main website, is there some code that allows me to show the latest posts?

I think I have have seen some MySQL/PHP code once before but I cannot find it anywhere.
Tagged:

Best Answer

  • camocamo New
    edited October 2011 Answer ✓
    Yes there is, here you go! Configure your domain etc where it says + how many

    <?php function vf_get_value($Key, &$Collection, $Default = FALSE) { $Result = $Default; if(is_array($Collection) && array_key_exists($Key, $Collection)) { $Result = $Collection[$Key]; } elseif(is_object($Collection) && property_exists($Collection, $Key)) { $Result = $Collection->$Key; } return $Result; } function vf_rest($Url) { try { $C = curl_init(); curl_setopt($C, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($C, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($C, CURLOPT_URL, $Url); $Contents = curl_exec($C); if ($Contents === FALSE) $Contents = curl_error($C); $Info = curl_getinfo($C); if (strpos(vf_get_value('content_type', $Info, ''), '/javascript') !== FALSE) { $Result = json_decode($Contents, TRUE); if (is_array($Result) && isset($Result['Exception']) && isset($Result['Code'])) { curl_close($C); throw new Exception($Result['Exception'], $Result['Code']); } } else { $Result = $Contents; } curl_close($C); return $Result; } catch (Exception $ex) { return $ex; } } function vf_combine_paths($paths, $delimiter = DS) { if (is_array($paths)) { $munged_path = implode($delimiter, $paths); $munged_path = str_replace(array($delimiter.$delimiter.$delimiter, $delimiter.$delimiter), array($delimiter, $delimiter), $munged_path); return str_replace(array('http:/', 'https:/'), array('http://', 'https://'), $munged_path); } else { return $paths; } } function vf_format_url($string) { $string = strip_tags(html_entity_decode($string, ENT_COMPAT, 'UTF-8')); $string = preg_replace('`([^\PP.\-_])`u', '', $string); // get rid of punctuation $string = preg_replace('`([^\PS+])`u', '', $string); // get rid of symbols $string = preg_replace('`[\s\-/+]+`u', '-', $string); // replace certain characters with dashes $string = rawurlencode(strtolower($string)); return $string; } function vf_recent_discussions() { /* ********************************************/ /* ************ Configure These **************/ /* ********************************************/ $title = 'Recent Discussions'; $categoryid = 0; $count = 5; $before_widget = '<div id="vf-widget-discussions" class="your_side_panel vf_widget_discussions">'; $before_title = '<h3>'; $after_title = '</h3>'; $after_widget = '</div>'; $url = 'http://www.domain.com/forum/'; $link_url = 'http://www.domain.com/forum/'; /* ********************************************/ $resturl = array($url, '?p=discussions.json'); if ($categoryid > 0) $resturl = array($url, '?p=categories/'.$categoryid.'.json'); $DataName = $categoryid > 0 ? 'DiscussionData' : 'Discussions'; // Retrieve the latest discussions from the Vanilla API $resturl = vf_combine_paths($resturl, '/'); $data = json_decode(vf_rest($resturl)); if (!is_object($data)) return; // These lines generate our output. Widgets can be very complex // but as you can see here, they can also be very, very simple. echo $before_widget . $before_title . $title . $after_title; echo '<ul>'; $i = 0; foreach ($data->$DataName as $Discussion) { $i++; if ($i > $count) break; echo '<li><a href="'.vf_combine_paths(array($link_url, 'discussion/'.$Discussion->DiscussionID.'/'.vf_format_url($Discussion->Name)), '/').'">'.$Discussion->Name.'</a></li>'; } echo '</ul>'; echo $after_widget; } ?> <div id="sidebar"> <ul class="sidebar_list"> <li> <?php vf_recent_discussions(); ?> </li> </div>

    source http://www.vanilla-wiki.info/Code/ShowRecentDiscussionsOnOtherPage

    See example

    http://www.camosreptiles.com.au/AskJacob.php#axzz1axPF8uLM

Answers

  • camocamo New
    edited October 2011 Answer ✓
    Yes there is, here you go! Configure your domain etc where it says + how many

    <?php function vf_get_value($Key, &$Collection, $Default = FALSE) { $Result = $Default; if(is_array($Collection) && array_key_exists($Key, $Collection)) { $Result = $Collection[$Key]; } elseif(is_object($Collection) && property_exists($Collection, $Key)) { $Result = $Collection->$Key; } return $Result; } function vf_rest($Url) { try { $C = curl_init(); curl_setopt($C, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($C, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($C, CURLOPT_URL, $Url); $Contents = curl_exec($C); if ($Contents === FALSE) $Contents = curl_error($C); $Info = curl_getinfo($C); if (strpos(vf_get_value('content_type', $Info, ''), '/javascript') !== FALSE) { $Result = json_decode($Contents, TRUE); if (is_array($Result) && isset($Result['Exception']) && isset($Result['Code'])) { curl_close($C); throw new Exception($Result['Exception'], $Result['Code']); } } else { $Result = $Contents; } curl_close($C); return $Result; } catch (Exception $ex) { return $ex; } } function vf_combine_paths($paths, $delimiter = DS) { if (is_array($paths)) { $munged_path = implode($delimiter, $paths); $munged_path = str_replace(array($delimiter.$delimiter.$delimiter, $delimiter.$delimiter), array($delimiter, $delimiter), $munged_path); return str_replace(array('http:/', 'https:/'), array('http://', 'https://'), $munged_path); } else { return $paths; } } function vf_format_url($string) { $string = strip_tags(html_entity_decode($string, ENT_COMPAT, 'UTF-8')); $string = preg_replace('`([^\PP.\-_])`u', '', $string); // get rid of punctuation $string = preg_replace('`([^\PS+])`u', '', $string); // get rid of symbols $string = preg_replace('`[\s\-/+]+`u', '-', $string); // replace certain characters with dashes $string = rawurlencode(strtolower($string)); return $string; } function vf_recent_discussions() { /* ********************************************/ /* ************ Configure These **************/ /* ********************************************/ $title = 'Recent Discussions'; $categoryid = 0; $count = 5; $before_widget = '<div id="vf-widget-discussions" class="your_side_panel vf_widget_discussions">'; $before_title = '<h3>'; $after_title = '</h3>'; $after_widget = '</div>'; $url = 'http://www.domain.com/forum/'; $link_url = 'http://www.domain.com/forum/'; /* ********************************************/ $resturl = array($url, '?p=discussions.json'); if ($categoryid > 0) $resturl = array($url, '?p=categories/'.$categoryid.'.json'); $DataName = $categoryid > 0 ? 'DiscussionData' : 'Discussions'; // Retrieve the latest discussions from the Vanilla API $resturl = vf_combine_paths($resturl, '/'); $data = json_decode(vf_rest($resturl)); if (!is_object($data)) return; // These lines generate our output. Widgets can be very complex // but as you can see here, they can also be very, very simple. echo $before_widget . $before_title . $title . $after_title; echo '<ul>'; $i = 0; foreach ($data->$DataName as $Discussion) { $i++; if ($i > $count) break; echo '<li><a href="'.vf_combine_paths(array($link_url, 'discussion/'.$Discussion->DiscussionID.'/'.vf_format_url($Discussion->Name)), '/').'">'.$Discussion->Name.'</a></li>'; } echo '</ul>'; echo $after_widget; } ?> <div id="sidebar"> <ul class="sidebar_list"> <li> <?php vf_recent_discussions(); ?> </li> </div>

    source http://www.vanilla-wiki.info/Code/ShowRecentDiscussionsOnOtherPage

    See example

    http://www.camosreptiles.com.au/AskJacob.php#axzz1axPF8uLM
  • edited October 2011
    Thank you but is there a way for it to show when a new reply has been made? It's pretty useless to returning visitors otherwise.
  • camocamo New
    edited October 2011
    You mean with the yellow highlighting?
    No idea how you'd do that mate, but you never mentioned that in your first post.
    In anycase, its the 'x' most recent, so they are always going to have relatively new post anyway unless your forums ghost town. If your forums really big and/or really busy, they wont stay there long enough for that to be an issue anyway. Unless you tweak the code to open a div or something and display the thread with ajax on the spot, then they click a discussion title and go there anyway, and they will see if theres any new post.
    You might be better off looking for a plugin or code to allow subscription, per discussion, or per category, or even per username, or just the whole forum, by email (opt in)
    sorry mate, best I can do. :)
  • I've used your code and greatly appreciate it, thanks.

    I suppose instead of the yellow a thing, maybe simply showing the number of replies for each discussion would achieve the same thing.

    Do you or anyone else have any idea how that can be done?
  • Ill have a look and see if I can work out how the highlighting is done (the coding not the css), but dont hold your breath ok. :)
  • Wow thanks, hope you succeed! :D

    I'm fine with the CSS part :)
  • I think line 82 is a problem if you try to filter to a topic, it has:
    $DataName = $categoryid > 0 ? 'DiscussionData' : 'Discussions';

    The API for 2.0.x has 'Discissions' as the dataname even when you have a category.
  • edited January 2012

    I'm trying to insert this code into a joomla module (is that how i'm supposed to do it?) I even worked out how to allow HTML, I only changed the URL to match my forum but i get the following error message;

    Forbidden

    You don't have permission to access /administrator/index.php on this server.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

    Bump. try again. :)

  • This code works perfectly but I was wondering if someone had the proper way to include additional information in the query/widget code. I would like include the author, perhaps number of replies, etc.

    Thanks,
    Dylan

  • peregrineperegrine MVP
    edited May 2013

    pull whatever you need out of the $Discussion object

        $disViewed = $Discussion->CountViews;
          $disFname = $Discussion->FirstName;
          $disTitle = $Discussion->Name;
          $disId = $Discussion->DiscussionID;
          $disUid = $Discussion->InsertUserID;
    
    pull whatever you need out of the $Discussion object 
    

    anything in here http://vanillaforums.org/discussion/17461.json is fair game.

    you can only pull out data that a guest would see.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • @peregrine said:
    anything in here http://vanillaforums.org/discussion/17461.json is fair game.

    you can only pull out data that a guest would see.

    It should be sanitised in a client side widget, you should strip all tags.

    grep is your friend.

  • How could I modify this to show most popular (views) and most replies threads? Has anything like this been done?

Sign In or Register to comment.