WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying recent comments outside WordPress (6 posts)

  1. mikejandreau
    Member
    Posted 6 years ago #

    So, this is complicated: There's two parts of our site, one part is in WordPress (posts/pages), the other is reviews, cast pages, and search results (which I wrote the code myself for).

    They share a centralized navigation system, where I want to include recent comments. I could use a Plugin, but that won't work for the pages outside of WordPress.

    So, as I did with the "Recent Posts" section, I'm trying to write my own code.

    I've got it working, without issue, but want to replace the "comment_content", with the permalink to the post. Since the permalink's not kept in the comment data (but linked via the ID, I'm assuming), I don't know how to do it.

    Here's the code I've got now, that lists the last 10, but excludes my two main authors:

    <?php
    // set your infomation.
    $dbhost='xxxxxxxxxx';
    $dbusername='xxxxxxxx';
    $dbuserpass='xxxxxxxxxxx';
    $dbname = 'xxxxxxxxxxxxx';
    
    // connect to the mysql database server.
    mysql_connect ($dbhost, $dbusername, $dbuserpass);
    //select the database
    mysql_select_db($dbname) or die('Cannot select database');
    if(isset($_GET['search']))
    {
    $search = $_GET['search'];
    }
    
    $keywords = explode(" ", $search);
    
    $query = "SELECT * FROM wordpress_comments WHERE comment_approved='1' AND user_id != '3' AND user_id != '2' ORDER BY comment_ID DESC LIMIT 10" ;
    
    $result = mysql_query($query) or die(mysql_error());
    ?>
    
    <?php
    while($row = mysql_fetch_array($result))
    {
    echo "<img src=\"../images/sub_bullet.gif\" alt=\"\"><a href=\"";
    echo("".$row["comment_author_url"]."");
    echo "\">";
    echo("".$row["comment_author"]."");
    echo "</a> ";
    echo("".$row["comment_content"]."");
    echo "<br>";
    }
    ?>

    I know it's not the most elegant code in the world, but it (sort of) works for what I want to do. Can someone point me in the right direction on how to get the permalink from the info available in the comments data?

  2. Adam Brown
    Member
    Posted 6 years ago #

    My preference for doing this is just to use the comments feed, and then use an open source package like Magpie RSS to display it on the page. That would have the added advantage of caching the recent comments rather than executing that query on every page load.

    Not a direct answer to your question, but then, I don't know the direct answer.

    Edit: Looks like you would want the "comment_post_ID" field from the comments table, then look up the post URL from the posts table.

  3. moshu
    Member
    Posted 6 years ago #

    I could use a Plugin, but that won't work for the pages outside of WordPress.

    Actually, it is easy to do: just add this to the top of that "outside" file, above everything else:

    <?php
    require('./path-to-your-blog/wp-blog-header.php');
    ?>

    and you can use any WP function (template tag) and most of the plugins.

  4. mikejandreau
    Member
    Posted 6 years ago #

    True, that'd work, but then it'd include all the content from my header.php file, which is already hard coded into the other page.

    I guess it's a solution. Not optimal for how the site's put together now, but I guess that could potentially work.

  5. mikejandreau
    Member
    Posted 6 years ago #

    Nevermind, I misread what you said. I'll give that a try, and see what happens. Thanks.

  6. mikejandreau
    Member
    Posted 6 years ago #

    Worked great, actually.

    I didn't even need to include my wp-blog-header.php file at the top of the "outside" WP pages.

    I included it at the top of a file, then included that file in the middle of my pages, where I wanted it. And it worked just fine.

    Thanks again for the help.

Topic Closed

This topic has been closed to new replies.

About this Topic