Display Topics / Replies in a custom .php file

ExpertPixels.com

Well-known member
Hey all,

Could anyone please help me with this.. or at least point me in the right direction to make the following possible. I am looking to display x amount of posts (lets say 5 for now).. on my custom home page in which i have created with a custom .php file that is displaying within the XF wrapper as my homepage..

I don't want / need a full blown cms setup, but would like to be able to display a simple "Latest News" section on my front page, these news items would be posted in the forums within a private forum or normal forum and i could then display the topics or replies as single items on the front page.. without complicating things.. basically something similar to xenPorta 9http://8wayrun.com).

A little like how the xenPorta front page portal posts are shown is what i am really looking to do but without going to the extent of creating a addon for this.

Cheers in Advance to any assistance on this topic.. I understand a lot of people posts questions here and are never seen or heard from again once they get what they want, i would like to point out i am not like that and will be contributing to xf myself as best i can.

Regards, Darren
 
Cheers for that.. I had actually considered doing that with a few other addons but thought i would respect the authors work in the sense that if they came up with that on their own i would effectivley being copying their work.. I was hoping there was a official / correct method to this.

Thanks anyways i will have a look as it might lead me in the right direction.
 
Cheers for that.. I had actually considered doing that with a few other addons but thought i would respect the authors work in the sense that if they came up with that on their own i would effectivley being copying their work.. I was hoping there was a official / correct method to this.

Thanks anyways i will have a look as it might lead me in the right direction.

i don't see at all as copying their work. They released the code for free to use.
 
It's way too advanced for me by the looks of it it's using DB queries which ive not even touched on.. I will have to start looking for another method / in a different direction.. Thanks anyways for your assistance.

Regards, Darren
 
@xf_phantom if you have any info / advice on how i can make about 4-5 topics from a certain forum appear within a custom .php file / my home page that would greatly appreciated

I am using a custom .php file to server my homepage via a template callback .. just not sure where to start in terms of what code i should be adding to pull this data / info through.

Regards, Darren
 
PHP:
//include your bootstrap/initialization things here...
// in my case:

require_once 'library\Core\bootstrap.php';


$threadModel = XenForo_Model::create('XenForo_Model_Thread');

$options = array(
    'limit' => 5
);

$threads = $threadModel->getThreadsInForum(2,array(), $options);
foreach ($threads AS $threadId => $thread){
    if (!$threadModel->canViewThread($thread,$thread)){
        unset($threads[$threadId]);
    }
}

var_dump($threads);
 
@xf_phantom Thanks matey :) appreciate your help.. Still not quite getting how / there to add the forum name for the threads to be pulled from but i will keep playing around with this, Thanks again.

Regards, Darren
 
ok here is where i am at with this:

This is the .php file that is my front page / what is displayed under my Home Tab:

PHP:
<?php
/**
******* http://expertpixels.com *******

- Template:    Default Template
- Usage:    Copy & Rename this template to suit your needs
- Notes:    Load any custom .css first, to be used on a per page basis
******* http://expertpixels.com *******
**/
class Expertpixels_Templates_FrontPage{
    public static function getHtml(){
   
include 'library/Expertpixels/Templates/news.php';    

    }
}

Now the content of news.php:

PHP:
<?php

$threadModel = XenForo_Model::create('XenForo_Model_Thread');
$options = array(
    'limit' => 5
);
$threads = $threadModel->getThreadsInForum(4,array(), $options);
foreach ($threads AS $threadId => $thread){
    if (!$threadModel->canViewThread($thread,$thread)){
        unset($threads[$threadId]);
    }
}
var_dump($threads);

Now this is what is displaying on my front page:

jEgr92n.png


I am not sure what / where i am going wrong.. but i would love to work out how to make work.

A HUGE THANKS in advance to anyone who can assist me with this / help me get this working.

Regards, Darren
 
The last line of your php file has var_dump() in it. This prints out the contents of a variable and explains what's in it (types, lengths, etc.). Remove it and that will go away.
 
You'll need to iterate through the threads and echo them put. Do you have the HTML you'd like to print out? If you look at the screenshot you posted, that's the information you will have available to you.
 
This might help:

PHP:
  /* Script to pull the latest posts from the XF Forum */   
     
   $db = XenForo_Application::getDb();
   
   if (!$db) {
   
     die('This script did not connect to the database' . mysql_error());
   
   }
   
   $thread_qry = "
       SELECT * FROM `xf_thread`
    ORDER BY `last_post_date` DESC
    LIMIT 5
   
   ";
   
   $threads = XenForo_Application::get('db')->fetchAll($thread_qry);
   
   foreach ($threads AS $thread)
   {

   
    echo ("<div class='entry-meta'><a href='path/to/community" . XenForo_Link::buildPublicLink('threads', $thread) .  "' >" . XenForo_Helper_String::wholeWordTrim($thread['title'], 48) . "</a> <span style='float:right; margin-right:60px'>Viewed: "  .$thread['view_count'] .  "</span><br /></div>"); // Echo the title with a link.
   
   }

Notice the echo statement is used rather than the var dump.
 
then
PHP:
<?php

$threadModel = XenForo_Model::create('XenForo_Model_Thread');$options = array('limit' => 5);$threads = $threadModel->getThreadsInForum(4,array(), $options);
foreach ($threads AS $threadId => $thread){
if ($threadModel->canViewThread($thread,$thread)){
echo ("<div class='entry-meta'><a href="' . XenForo_Link::buildPublicLink('canonical:threads', $thread) . "' >" . XenForo_Helper_String::wholeWordTrim($thread['title'], 48) . "</a> <span style='float:right; margin-right:60px'>Viewed: " .$thread['view_count'] . "</span><br /></div>"); // Echo the title with a link.
  
}
}
 
Cheers @LPH & @xf_phantom after a quick test i have something to work with now, the above now has the thread title and the post content... Now if only i was able to add some options like.. limit the amount of text per post add a image per posts and also say display a max of 5 threads that would be awesome but i wonder who would be smart enough to be able to help me with that... hehe

Cheers you guys made my day :)

Regards, Darren
 
Cheers @LPH & @xf_phantom after a quick test i have something to work with now, the above now has the thread title and the post content... Now if only i was able to add some options like.. limit the amount of text per post add a image per posts and also say display a max of 5 threads that would be awesome but i wonder who would be smart enough to be able to help me with that... hehe

Cheers you guys made my day :)

Regards, Darren
@ExpertPixels.com
You are free to use the code in my portal any way you want. Obviously, you can't sell it... but feel free. No matter what you will need to do a .db query to get the posts, whether you use the Zend DB engine that the XenForo application is using or do a quick 1-off query.

If you want a super stripped down version where you just drop it into a stand - alone page. I have that available also.
Or if you just need help feel free to ask.

--ET
 
Cheers @LPH & @xf_phantom after a quick test i have something to work with now, the above now has the thread title and the post content... Now if only i was able to add some options like.. limit the amount of text per post add a image per posts and also say display a max of 5 threads that would be awesome but i wonder who would be smart enough to be able to help me with that... hehe

Cheers you guys made my day :)

Regards, Darren
The above code by xf_phantom will limit it to 5 threads, are you receiving more? The second snippet from xf_phantom (fixed below) should trim the content to 48 words:
PHP:
<?php

echo ('<div class="entry-meta"><a href="' . XenForo_Link::buildPublicLink('canonical:threads', $thread) . '">' . XenForo_Helper_String::wholeWordTrim($thread['title'], 48) . '</a> <span style="float:right; margin-right:60px">Viewed: ' . $thread['view_count'] . '</span><br /></div>');

If you provide the actual code you are using, I may be able to provide more help.
 
Top Bottom