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.

Embedding forum category on website

I am currently building a website with a Vanilla forum built in. On the homepage of the website, I want to display the top 5 most recent discussions within the "News" category of the forum.

Ideally, I would want to retrieve the first 100 words or so of each of those 5 most recent discussions with a link to read the entire discussion at the end (sort of like a snippet on the front cover of a newspaper).

Is this possible to do using Vanilla's API, and if so, how would I do it?

My website uses an Apache server with PHP and MySQL.

Tagged:

Comments

  • Here's a screenshot of what I am talking about.

    As you can see on the bottom left, I wanted to have a section for "News."

  • edited May 2014

    You could post the RSS feed for Vanilla discussions.

    Just get a feed script and paste the url of the feed and then put that in your website.

    http://jquery-plugins.net/FeedEk/FeedEk_demo.html

    You can add the feed for all categories or several feeds one for each category and set up how much you want to show by way of character limit. This is a great plugin that I made into a vanilla plugin for here.

    It is simple to set up and works like magic

  • Welcome to the community!

    What version number of Vanilla are you running?

    Is your main site running on top of Vanilla/Garden as well?

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • @hgtonight said:
    Welcome to the community!

    What version number of Vanilla are you running?

    Is your main site running on top of Vanilla/Garden as well?

    I am running version 2.1.

    I discovered a solution to it myself by connecting to the MySQL database and manually retrieving the all the discussions from my "News" category. Here's the code I used:

    <?php 
        function getNews() {
            // Create connection
            $con=mysqli_connect("domain","user","password","vanilla");
    
            // Check connection
            if (mysqli_connect_errno()) {
              echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }
            $data = mysqli_query($con, "SELECT * FROM gdn_discussion WHERE CategoryID=2 ORDER BY DateInserted DESC LIMIT 5") or die(mysql_error());
     ?>
            <div id="newsfeed">
                  <h3>News</h3>
                  <?php 
                  while ($info = mysqli_fetch_array( $data )) {
                    Print "<article>";
                    Print substr( strip_tags( $info['Body'] ), 0, 500 );
                    Print " <a href=\"/forum/index.php?p=/discussion/".$info['DiscussionID']."\">[...]</a>";
                    Print "</article>";
                  }
                  mysqli_close($con);
                   ?>
            </div>
     <?php 
     }
      ?>
    

    Is it generally considered bad practice to do this? Also, I'm not very experienced with PHP and SQL; but I'm pretty sure I used deprecated code in the line that says
    $data = mysqli_query($con, "SELECT * FROM gdn_discussion WHERE CategoryID=2 ORDER BY DateInserted DESC LIMIT 5") or die(mysql_error());. Do you know the proper way to catch that error?

  • Why go through that trouble when your main site is not Vanilla? This is what feed readers are for .... :S

  • @vrijvlinder said:
    Why go through that trouble when your main site is not Vanilla? This is what feed readers are for .... :S

    I just figure it's more elegant from a programmer's perspective to use PHP when it's appropriate. I find that Javascript is better suited for editing pages on the fly, whereas PHP is designed to retrieve data as the page loads.

  • R_JR_J Admin

    @LimitedWard said:
    Is it generally considered bad practice to do this?

    I don't know about that, but I personally would try to avoid that for the following reasons:
    1. there is no permission check in your code
    2. you disregard announcements, sunken discussions and deleted discussions
    3. whatever the future might bring to Vanilla, your couldn't respect it
    4. you are reinventing the wheel. Vanilla comes with functions that help you achieve what you want and so you wouldn't have to mess around with mysqli_query

    Nevertheless it has often been said by wiser man than me, that loading the complete framework on top of your page for achieving only simple things isn't always a good idea. So you should answer hgtonights question if your site is already running on Vanilla.

    If yes, use Vanillas build in functionality
    If no, a) you might load Vanilla nevertheless and use its functionality and if you do not want to do that,
    b) I would suggest you create a view and use that for direct queries. That way you will only have to change the view if future will bring any enhancements of the database (additional permission considerations, other handling of sunken discussions/announcements on your portal site, etc) that you have to take into consideration.

  • If your main site does not run Vanilla:

    I would query the Vanilla db through an ajax request OR update a cached copy when editing/adding a discussion to your news category. Then include the cached view as a regular PHP include on your main site.

    If you run something built on the Garden framework, you just need to create a new view for the categories controller and set that as your default controller.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

Sign In or Register to comment.