1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Display Topics / Replies in a custom .php file

Discussion in 'XenForo Development Discussions' started by ExpertPixels.com, Jun 28, 2013.

  1. ExpertPixels.com

    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
     
  2. Matthew Hawley

    Matthew Hawley Well-Known Member

  3. ExpertPixels.com

    ExpertPixels.com Well-Known Member

    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.
     
  4. Matthew Hawley

    Matthew Hawley Well-Known Member

    i don't see at all as copying their work. They released the code for free to use.
     
  5. ExpertPixels.com

    ExpertPixels.com Well-Known Member

    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
     
  6. xf_phantom

    xf_phantom Well-Known Member

    You don't need db queries if you use the already existing xenforo models to fetch the data
     
  7. ExpertPixels.com

    ExpertPixels.com Well-Known Member

    @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
     
  8. xf_phantom

    xf_phantom Well-Known Member

    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);

     
  9. ExpertPixels.com

    ExpertPixels.com Well-Known Member

    @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
     
  10. ExpertPixels.com

    ExpertPixels.com Well-Known Member

    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:

    [​IMG]

    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
     
  11. LPH

    LPH Well-Known Member

    ExpertPixels.com likes this.
  12. ExpertPixels.com

    ExpertPixels.com Well-Known Member

    @LPH What does that mean lmao ?? sorry i was using the code provided above from @xf_phantom sorry to be such a hassle still trying to suss this out.

    Darren
     
  13. Jeremy

    Jeremy XenForo Moderator Staff Member

    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.
     
    ExpertPixels.com likes this.
  14. ExpertPixels.com

    ExpertPixels.com Well-Known Member

    I have removed this.. I still can not get any forums / threads to display.. Really struggling with this one. @King Kovifor Thanks for your assistance !!
     
  15. Jeremy

    Jeremy XenForo Moderator Staff Member

    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.
     
  16. LPH

    LPH Well-Known Member

    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.
     
  17. xf_phantom

    xf_phantom Well-Known Member

    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.
      
    }
    }
     
  18. ExpertPixels.com

    ExpertPixels.com Well-Known Member

    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
     
  19. EasyTarget

    EasyTarget Well-Known Member

    @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
     
    ForestForTrees likes this.
  20. Jeremy

    Jeremy XenForo Moderator Staff Member

    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.
     
    LPH and ExpertPixels.com like this.

Share This Page