Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Topic started by: NanoSector on October 15, 2012, 04:22:55 PM

Title: [Tip] Show the latest WordPress posts inside SMF
Post by: NanoSector on October 15, 2012, 04:22:55 PM
Perhaps you've set up a portal.

Prerequisites:
- An SMF installation
- A WordPress installation
- A place to put code (like in a Portal block)

Step 1: Locating wp-load.php
You need to locate the exact location of the wp-load.php file of your WordPress installation. Most of the time it's in the root of the installation.

Step 2: Decide how many posts you want to show
Maybe you want to show 3 posts, maybe 4, or even 5. Now's the time to decide - But you can always change it later on.

Step 3: Decide to show the blog post itself or not
This is not so easy to change later on - now's the time to decide.

Step 4: Include the wp-load.php file
I'm assuming we are writing it in a portal block.

Put the following code in the code box (make sure it's set to PHP code):
require('/path/to/your/blog/wp-load.php');

For me, this is
require('/home/mapcmsco/public_html/blog/wp-load.php');

This piece of code gets the WordPress environment up and running.

Step 5: Grab the posts from WordPress
You have decided how many posts you want to show.

Place this piece of code in the block, after the previous piece, on a new line:
$recent_posts = get_posts(array('numberposts' => x));

Replace x with the amount of posts you want to show. I want to show 3 posts, so I put:
$recent_posts = get_posts(array('numberposts' => 3));

My code so far is this:
require('/home/mapcmsco/public_html/blog/wp-load.php');
$recent_posts = get_posts(array('numberposts' => 3));


Step 6: Displaying the posts
This is the most tricky part. If you want to customize this to your needs, post in this topic and I'll guide you - I'll keep this step basic.

If you want to show only the titles of the posts (with a link), add this piece of code:
foreach ($recent_posts as $post)
{
        echo '<a href="' . $post->guid . '">' . $post->post_title . '</a><br />';
}


Assuming the posts are called Post 1, Post 2 and Post 3, you'll get this output:
QuotePost 1 (#post_)
Post 2 (#post_)
Post 3 (#post_)

Good enuf'? Continue. Too simple? Read on!

If you want to display the dates along with the title, in brackets, like Post 1 (31th September 2012, 3:32:51 AM GMT), put this code:
foreach ($recent_posts as $post)
{
        echo '<a href="' . $post->guid . '">' . $post->post_title . '</a> (' . $post->post_date_gmt . ' GMT)<br />';
}


Your WordPress installation controls how the date is displayed. Check your settings for that.

If you want to display the amount of comments with it (like Post 1 (1 comment(s))), put this code:
foreach ($recent_posts as $post)
{
        echo '<a href="' . $post->guid . '">' . $post->post_title . '</a> (' . $post->comment_count . ' comment(s))<br />';
}


If you want to display the blog post itself, you'll need to think of a layout you want to put it in - it's not going to work in a sidebar, sadly.

My code would be:
require('/home/mapcmsco/public_html/blog/wp-load.php');
$recent_posts = get_posts(array('numberposts' => 3));
foreach ($recent_posts as $post)
{
        echo '<a href="' . $post->guid . '">' . $post->post_title . '</a> (' . $post->comment_count . ' comment(s))<br />';
}


Since I want to show the amount of comments with it.

If you want any customized layouts or more possibilities, please post in this topic and I'll try to help you :)

The actual code I use on my site
I use this in the templates, my sources are below:
echo '<div class="cat_bar">
                <h3 class="catbg">
                        Latest blog posts
                </h3>
        </div>';
        foreach ($context['rposts'] as $post)
        {
                echo '
        <div class="title_barIC">
                <h4 class="titlebg">
                        <span class="floatleft"><a href="', $post->guid, '">', $post->post_title, ' (', $post->post_date_gmt, ' GMT)</a></span>
                        <span class="floatright"><a href="', $post->guid, '#comments">', $post->comment_count . ($post->comment_count == 1 ? ' comment' : ' comments'), '</a></span>
                </h4>
        </div>
        <div class="windowbg">
                <span class="topslice"><span></span></span>
                <div style="margin-left:10px;margin-right:10px">
                        ', parse_bbc($post->post_content), '
                </div>
                <span class="botslice"><span></span></span>
        </div>';
        }
       
        echo '
        <a href="http://map3cms.co.cc/blog">Visit the blog to read more...</a>';


Check my frontpage (http://map3cms.co.cc/) to see how it looks.

Source-side is this happening:
        // Lets get WP rollin'.
        require($dir . '/blog/wp-load.php');
       
        // Gather the posts.
        $context['rposts'] = get_posts(array('numberposts' => 3));


$dir is the variable my site framework uses to indicate the working directory.
Title: Re: [Tip] Show the latest WordPress posts inside SMF
Post by: mrintech on October 15, 2012, 04:37:59 PM
Great! (https://www.simplemachines.org/community/proxy.php?request=http%3A%2F%2Fwww.katzy.dsl.pipex.com%2FSmileys%2Fc014.gif&hash=f74820123053ff9301b57a2ded5672bfa96b900b)

Will try this with post from Test WP Blog

Thanks! :)
Title: Re: [Tip] Show the latest WordPress posts inside SMF
Post by: NanoSector on October 15, 2012, 04:40:19 PM
Cool! :)

I've already done this myself -> http://map3cms.co.cc/, bottom of the page.
Title: Re: [Tip] Show the latest WordPress posts inside SMF
Post by: mrintech on October 16, 2012, 02:08:13 AM
Just a quick question: Is it possible to show only WordPress excerpts?

Offtopic:

Why you are using .co.cc ??? You are hosting your site on a SUB-DOMAIN of .co.cc (map3cms.co.cc) !

.co.cc sub-domains hardly ranks in search results (Google). If possible, try to get a TLD ASAP :)
Title: Re: [Tip] Show the latest WordPress posts inside SMF
Post by: NanoSector on October 16, 2012, 02:54:46 AM
Do you mean only one paragraph of your post?

OT: cause it's free. I'm moaning I don't get any traffic all the time :P
Title: Re: [Tip] Show the latest WordPress posts inside SMF
Post by: mrintech on October 16, 2012, 03:44:47 AM
Quote from: Yoshi2889 on October 16, 2012, 02:54:46 AM
Do you mean only one paragraph of your post?

OT: cause it's free. I'm moaning I don't get any traffic all the time :P

Yeah! something like that OR first 200 to 300 words: http://codex.wordpress.org/Excerpt

OT: Majority of traffic to any site comes from Google. .co.cc sub-domains don't rank at all. Try to get a TLD and see the difference yourself ;)
Title: Re: [Tip] Show the latest WordPress posts inside SMF
Post by: NanoSector on October 16, 2012, 05:09:13 AM
AFAIK you'd use $post->post_excerpt, but it's empty even if I have a <!-- more --> tag set... :-\
http://codex.wordpress.org/Function_Reference/get_post#Return

OT: True. Maybe I'll purchase premium hosting and a premium domain later on, my site works fine now :)
Title: Re: [Tip] Show the latest WordPress posts inside SMF
Post by: kingston250 on December 20, 2012, 09:56:09 AM
Really nice post. I will try to do this. I hope this will be helpful for me.
Title: Re: [Tip] Show the latest WordPress posts inside SMF
Post by: Crozz on December 21, 2012, 09:21:46 AM
Nice tip, a bunch of thanks for that, I did want something like this.
Title: Re: [Tip] Show the latest WordPress posts inside SMF
Post by: NanoSector on December 21, 2012, 09:27:39 AM
Thanks both, and good luck with applying this trick :)
Title: Re: [Tip] Show the latest WordPress posts inside SMF
Post by: jonno1 on March 27, 2013, 05:52:23 AM
This causes the post times on my forum to be incorrect on anything after the TP Block. Any ideas, it's got me foxed.