We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48777
    • 11 Posts
    Hi,

    is there a solution to embed a thread list of a defined board on an other page?

    For example: I want to Display the list of threads of my board "News" on the first page of my Site.

    Is there a Chance?

    Tanks!
      • 46220
      • 66 Posts
      There should be. I am no coder, but if you look at this thread http://forums.modx.com/thread/91788/recent-posts-threads-for-discuss-by-rimotevst-kanev#dis-post-508381, it should be possible.
        • 48777
        • 11 Posts
        Quote from: gormytorysh at Sep 09, 2014, 09:46 PM
        There should be. I am no coder, but if you look at this thread http://forums.modx.com/thread/91788/recent-posts-threads-for-discuss-by-rimotevst-kanev#dis-post-508381, it should be possible.

        Thank you! That helped me out! =)
          • 46886
          • 1,154 Posts
          If you change the code to limit the output to threads in one board, as you say you want, pls do share your code with us, so we can learn smiley
            • 48777
            • 11 Posts
            Hey!

            Of course, here you got it! smiley

            This is the extended version... About half of the code is not needed, but I still had no time to remove the not needed parts. So you have to figure out, in case of my settings on top, which parts are the important.
            I structured it a bit and made it more readable so it should be clear! wink

            The main extension is that we now have a filter on "board_id". So only posts of this board will be listed!

                // **************************************************
                // *** SETTINGS
                // **************************************************
                $board_id = 5; // NEWS
                
                // POST SETTINGS BELOW
                $show_posts = '1'; //Set to 1 if you want to show posts
                $posts_root_only = '1'; // Only root posts (fist post of a thread (parent = 0))
                $posts_last_added = '1'; //Set to 1 if you want to show last added posts
                $posts_dynamic = '0'; //Set to 1 if you want to show dynamicly posts from current board
                 
                // THREADS SETTINGS BELOW
                $show_threads = '0'; //Set to 1 if you want to show threads
                // ONLY STICKY THREADS
                $threads_sticky = '1'; // Set this to 1 if you want only sticky threads
                // SELECT ONLY 1 OF 3 OPTIONS BELOW
                $threads_last_posted = '1'; //Set to 1 if you want to show lastly active threads
                $threads_most_viewed = '0'; //Set to 1 if you want to show most viewed threads
                $threads_most_reply = '0'; //Set to 1 if you want to show threads with most reply's
             
                $order = 'DESC';  //Set ASC for A->Z or DESC for Z->A
                $limit = '5'; //Set limit count for results outputed which to be showed
                $letters_limit = '120'; //Set letters limit for description of posts messages
                $user_real_name = '1'; //Set to 1 if you want to show First Last names, set to 0 for username
                 
                
                // **************************************************
                // *** QUERY
                // **************************************************
                if($show_threads == '1'){
                    $c = $modx->newQuery('disThread');
                }else{
                    $c = $modx->newQuery('disPost');
                }
                
                // **************************************************
                // *** SORTATION
                // **************************************************
                if(($show_posts == '1') AND ($posts_last_added == '1')){
                    $c->sortby('createdon',$order);
                }else{}
                
                if(($show_threads == '1') AND ($threads_last_posted == '1')){
                    $c->sortby('post_last_on',$order);
                }else{}
                
                if(($show_threads == '1') AND ($threads_most_viewed == '1')){
                    $c->sortby('views',$order);
                }else{}
                
                if(($show_threads == '1') AND ($threads_most_reply == '1')){
                    $c->sortby('replies',$order);
                }else{}
                
                // **************************************************
                // *** FILTER
                // **************************************************
                if($show_threads == '1'){   
                    if($board_id != 0){
                        $c->where(array( 'board' => $board_id, 'private' => '0' ));
                    }else{
                        $c->where(array( 'private' => '0' ));
                    }
                }else{
                    // If specific board id was defined in parameters
                    if($board_id != 0){
                        // Get all threads of a board and all posts of these threads
                        // Because: Posts will not be deleted when a thread is deleted! -.-
                        $t = $modx->newQuery('disThread');
                        $t->where(array('board' => $board_id));
                        
                        if($threads_sticky == '1'){
                            $t->where(array('sticky' => '1'));
                        }
                        
                        $thread_list = array();
                        
                        $threads = $modx->getCollection('disThread',$t);
                        foreach ($threads as $thread) {
                            array_push($thread_list, array('thread' => $thread->get('id')));
                        }
                        
                        $c->where($thread_list, xPDOQuery::SQL_OR);
                    }else{
                        //If we are in main board page
                        if((strip_tags($_GET['board']) != '') AND ($posts_dynamic == '1')){
                            $c->where(array('board' => strip_tags($_GET['board'])));
                        }else{}
                        
                        //If we are in thread page
                        if((strip_tags($_GET['thread']) != '') AND ($posts_dynamic == '1')){
                            $t = $modx->newQuery('disThread');
                            $t->where(array('id' => strip_tags($_GET['thread'])));
                            $t->limit(1,0);
                            
                            $boards = $modx->getCollection('disThread',$t);
                            foreach ($boards as $board) {
                                $c->where(array('board' => $board->get('board')));
                            }
                        }else{}
                    }
                    
                    if($posts_root_only == '1'){
                        $c->where(array( 'parent' => '0' ));
                    }
                }
                
                // **************************************************
                // *** LIMIT
                // **************************************************
                $c->limit($limit,0);
                 
                // **************************************************
                // *** GET THE RESULT
                // **************************************************
                if($show_threads == '1'){
                    $resources = $modx->getCollection('disThread',$c);
                }else{
                    $resources = $modx->getCollection('disPost',$c);
                }
                
                // **************************************************
                // *** BUILD OUTPUT
                // **************************************************
                $is_first = true;
                $output = '';
                
                foreach ($resources as $res) {
                    if($show_threads == '1'){
                        $output .= '<li class="rthread"><a href="forum/thread/'.$res->get('id').'/'.strtolower($res->get('title')).'">'.$res->get('title').'</a>';
                        $a = $modx->getObject('disUser', array('id' => $res->get('author_first')));
                        $output .= ' by <a class="rauthor" href="forum/u/'.$a->get('username').'/">';
                        
                        if($user_real_name == '1'){
                            $output .= $a->get('name_first').' '.$a->get('name_last').'</a>';
                        }else{
                            $output .= $a->get('username').'</a>';
                        }
                        
                        $output .= '</li>';
                    }else{
                        // **************************************************
                        // *** COLLECT ALL INFORMATIONS (just for clear view)
                        // **************************************************
                        $thread = $modx->getObject('disThread', array('id' => $res->get('thread')));
                        $author = $modx->getObject('disUser', array('id' => $res->get('author')));
                        
                        $thread_title = $res->get('title');
                        $thread_content = $res->getContent();
                        
                        $thread_author = $author->get('username');
                        $thread_created_date = date("j. F Y");
                        $thread_created_time = date("H:i");
                        $thread_replies = $thread->get('replies');
                        $thread_reply_last = $thread->get('post_last');
                        
                        // **************************************************
                        // *** BUILD HTML
                        // **************************************************
                        if($is_first == true){
                            $is_first = false;
                            $output .= '<div class="pe-news-entry first block">';
                        }else{
                            $output .= '<div class="pe-news-entry block">';
                        }
                        
                            $output .= '<div class="pe-news-entry-header block">';
                                $output .= '<div class="pe-news-entry-field title block">';
                                    $output .= "<i class=\"fa fa-chevron-down\"></i><a href=\"/forum/thread/" . $res->get('thread') . "\">" . $thread_title . "</a>";
                                $output .= '</div>';
                            $output .= '</div>';
                            $output .= '<div class="pe-news-entry-main block">';
                                $output .= '<div class="pe-news-entry-field content block">';
                                    $output .= $thread_content;
                                $output .= '</div>';
                            $output .= '</div>';
                            $output .= '<div class="pe-news-entry-footer block">';
                                $output .= '<div class="pe-news-entry-field author block">';
                                    $output .= "<a href=\"/members.html?u=$thread_author\">" . $thread_author . "</a>";
                                $output .= '</div>';
                                $output .= '<div class="pe-news-entry-field date block">';
                                    $output .= "am " . $thread_created_date . " um " .$thread_created_time . "Uhr";
                                $output .= '</div>';
                                $output .= '<div class="pe-news-entry-field comments block">';
                                    $output .= "<a href=\"/forum/thread/" . $res->get('thread') . "\">";
                                    if($thread_replies == '1'){
                                        $output .= $thread_replies . " Kommentar";
                                    }else{
                                        $output .= $thread_replies . " Kommentare";
                                    }
                                    $output .= "</a>";
                                $output .= '</div>';
                            $output .= '</div>';
                        $output .= '</div>';
                        
                        /*
                        $output .= $thread_created_date . $thread_created_time;
                        
                        $output .= '<li class="rpost"><a href="forum/thread/'.$res->get('thread').'/'.strtolower($res->get('title')).'#dis-post-'.$res->get('id').'">'.$res->get('title').'</a>';
                        $output .= '<p>'.mb_substr($res->get('message'), 0, $letters_limit).'</p>';
                        $a = $modx->getObject('disUser', array('id' => $res->get('author')));
                        $output .= ' by <a class="rauthor" href="forum/u/'.$a->get('username').'/">';
                        
                        if($user_real_name == '1'){
                            $output .= $a->get('name_first').' '.$a->get('name_last').'</a>';
                        }else{
                            $output .= $a->get('username').'</a>';
                        }
                        
                        $output .= '</li>';
                        
                        $output .= $res->getContent();
                        */
                    }
                }
                
                return $output;
            


            So I hope this helps you out! =)
              • 48777
              • 11 Posts
              Noticed a little bug in line 152 & 153!

              Changed it to:

              $thread_created_date = date("j. F Y", strtotime($res->get('createdon')));
              $thread_created_time = date("H:i", strtotime($res->get('createdon')));
              
                • 46220
                • 66 Posts
                Very nice and might be useful for me in a later stadium as well smiley. Perhaps you know how to solve my problem (or point me into the right direction) I am having explained here: http://forums.modx.com/thread/91788/recent-posts-threads-for-discuss-by-rimotevst-kanev#dis-post-508663?
                  • 46886
                  • 1,154 Posts
                  Wow that's great! Thanks for sharing your work!!

                  Now let me take a second to point out one weakness in Rimotevst's tool. He didn't know MODX well at that time, so he didn't know that normally we will use a chunk to handle the output of the tool, he hardcoded it in there, which is a little less extensible. Just an FYI.

                  Right now we are trying to figure out how to make a filter for the post output, where private messages are removed from the collection. But it seems a bit tough because posts don't have a private flag, only threads have a private flag. It shouldn't be too hard but we still can't get it, so far.
                    • 48777
                    • 11 Posts
                    That means you just want to get some Thread information of a post?!

                    Then look at line 145 in my example.
                    Here I get the thread of my post and then you can ask it for his private flag ...

                    $thread = $modx->getObject('disThread', array('id' => $res->get('thread')));
                    $thread_is_private = $thread->get('private');
                    


                    Or am I wrong? tongue
                      • 46886
                      • 1,154 Posts
                      Yes! This looks perfect! Wow you are so capable!

                      I didn't know we could so easily get the thread data of the posts, but it seems to be because of your newly-written code, you fixed that original weakness I think.