• Welcome to Tux Reports: Where Penguins Fly. We hope you find the topics varied, interesting, and worthy of your time. Please become a member and join in the discussions.
HOWTO: Show WordPress Blog Posts on XenForo Sidebar

HOWTO: Show WordPress Blog Posts on XenForo Sidebar

Compatible XenWord Versions
3.x
Compatible WP Versions
  1. 4.1
  2. 4.2
Compatible XF Versions
  1. 1.4
Visible Branding
No
A simple XenForo addon called XenLate is now available for download and installation.

DIY Instructions:

  1. Create a file called WPPosts.php.
  2. Copy and paste the code below into the file.
  3. Change the class to the directory structure you would like
    • If you keep the class as TRN_XenWord_Model then you must create the directories under the XenForo/library
  4. Change the path to wp-blog-header.php
  5. Modify the numberposts to meet your desires
  6. Save and Upload the file
  7. Go to XenForo admin panel templates
  8. Find the template ad_sidebar_below_visitor_panel
  9. Add the callback, adjust the path to the WPPosts file
  10. Add CSS to the EXTRA.css template in XenForo Style

WPPosts.php
PHP:
<?php
/**
* Filename: WPPosts.php
* Author: LPH
*/
class TRN_XenWord_Model_WPPosts extends XenForo_Model {
    public static function getHtml() {

        define( 'WP_USE_THEMES', false );
        define( 'DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT'] );

        // Change the path to the correct location
        require( DOCUMENT_ROOT . '/wp-blog-header.php' );

        $args          = array( 'numberposts' => '5', 'post_type' => 'post', 'post_status' => 'publish' );
        $LatestWPPosts = wp_get_recent_posts( $args );

        echo '<div class="section">
               <div class="secondaryContent">
                   <div class="visitorText">
                       <h3>Latest Blog Posts</h3>
                       <div class="LatestWPPosts">';

        foreach ( $LatestWPPosts as $LatestWPPost ) {

            $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $LatestWPPost['ID']));
            $thumbnail = get_the_post_thumbnail( $LatestWPPost['ID'], array( 50, 50) );

            if ( empty( $thumbnail ) ) {

                echo "<h4><a href='" . esc_url( get_permalink( $LatestWPPost['ID'] ) ) . "'>" . $LatestWPPost['post_title'] . '</a></h4>';

            } else {

                echo '<div class="recent_post">';
                echo '<div class="thumbnail">' . $thumbnail . '</div>';
                echo "<h4><a href='" . esc_url( get_permalink( $LatestWPPost['ID'] ) ) . "'>" . $LatestWPPost['post_title'] . '</a></h4><br />';
                echo '<div class="excerpt">' . $excerpt . '</div>';
                echo '</div>';

            }

        }

        echo '</div>
                       </div>
                   </div>
                </div>
            ';

    }
}

// End WPPosts.php

Callback
Code:
<xen:callback class="TRN_XenWord_Model_WPPosts" method="getHtml"></xen:callback>

CSS
Code:
div.recent_post {
    position: relative;
    padding-bottom: 25px;
    margin-top: 5px;
}

div.thumbnail {
    float: left;
    margin-right: 1em;
    margin-top: 2px;
}

div.excerpt {
    padding-bottom: 1em;
    padding-left: 5em;
    border-bottom: 1px solid grey;
}
Author
LPH
Views
125
First release
Last update
Rating
4.00 star(s) 1 ratings

More resources from LPH

Latest updates

  1. Updated code to include featured image

    New PHP and CSS code to include thumbnail of a featured image.

Latest reviews

simple and very helpful. WOuld be good if it can be made to work with [bd]Widgets too
Top