[Tip] Show the latest WordPress posts inside SMF

Started by NanoSector, October 15, 2012, 04:22:55 PM

Previous topic - Next topic

NanoSector

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 2
Post 3

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 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.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

mrintech

Great!

Will try this with post from Test WP Blog

Thanks! :)

NanoSector

My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

mrintech

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

NanoSector

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
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

mrintech

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

NanoSector

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 :)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

kingston250

Really nice post. I will try to do this. I hope this will be helpful for me.

Crozz

Nice tip, a bunch of thanks for that, I did want something like this.

NanoSector

Thanks both, and good luck with applying this trick :)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

jonno1

This causes the post times on my forum to be incorrect on anything after the TP Block. Any ideas, it's got me foxed.

Advertisement: