[SOLVED] Is it possible to add a couple blog posts to homepage?

Ask for help or provide support to other members.
Post Reply
nozaro33
Posts: 3
Joined: Sun Mar 14, 2021 3:37 am

[SOLVED] Is it possible to add a couple blog posts to homepage?

Post by nozaro33 »

I have a landing page homepage, and would like to add some blogposts to the front, where users can see before heading to the blog page. Is it possible to do this? I have ability to do some basic php coding.
Hope I'm clear enough! Thanks.

Version: WonderCMS 3.1.4
OneManLaptop
Developer
Posts: 19
Joined: Tue Mar 16, 2021 3:29 pm

Re: Is it possible to add a couple blog posts to homepage?

Post by OneManLaptop »

Hey nozaro, I'm not sure if this is possible to do natively, but I've had a lot of success using jQuery's .load functionality to grab blog articles and put them on my home page. Providing you're familiar with the basics of jQuery, you should have no problem grabbing some blog entries and placing them on your home page:

So what I did here was edit the essence theme (themes/essence/theme.php (line 60)) to wrap the blog articles in a div id called loadThisBlog:

Code: Select all

<section id="loadThisBlog" class="container mt-5 mb-5">
	<div class="row">
		<div class="col-lg-12 my-auto text-center padding40">
			<?= $Wcms->page('content') ?>
		</div>
	</div>
</section>
I then stored the url of the blog in a variable and used jQuery .load(); to load just the blog articles from the front page of the blog and prepend them into a div on my home page, which I created called blogIndex, using the following code:

Code: Select all

$('<div class="blogWrap"></div>').load(blogURL + " #loadThisBlog").prependTo(".blogIndex");
You can manipulate this however you want, if you just want to get the first one for example, you can use nth selectors:

Code: Select all

.load(blogURL + " #loadThisBlog .post:nth-of-type(1)")
Smarter people than me would be able to tell you how to do this with arrays to make it easier, but you can read more about load at:

https://api.jquery.com/load/#loading-page-fragments

Anyway, watch somebody come along now and tell you a really easy way to do it natively, haha. With a bit of experimenting though, you should be able to do what you want with this method. Good luck.
nozaro33
Posts: 3
Joined: Sun Mar 14, 2021 3:37 am

Re: Is it possible to add a couple blog posts to homepage?

Post by nozaro33 »

Thanks OneManLaptop! I really appreciate it!
I figured out another method, that I'll share here:
using php -> file_get_contents($Wcms->dataPath . '/simpleblog.json)
I grab the info directly form the json file, and display it on my homepage!

Alex
OneManLaptop
Developer
Posts: 19
Joined: Tue Mar 16, 2021 3:29 pm

Re: Is it possible to add a couple blog posts to homepage?

Post by OneManLaptop »

Haha, that's probably easier. :) I need to brush up on my JSON, I think it'll be a valuable skill if I'm working on this CMS!
OneManLaptop
Developer
Posts: 19
Joined: Tue Mar 16, 2021 3:29 pm

Re: Is it possible to add a couple blog posts to homepage?

Post by OneManLaptop »

Actually just whilst I've got a fellow Wonder CMS user here, have you tried adding images to the description area of the blog? Have you witnessed any glitching or strange behaviour where the image jumps out of place and makes a bit of a mess of the JSON? I've had it once or twice. I've been able to fix it but it does make me a bit nervous about handing the site over to clients to edit!
nozaro33
Posts: 3
Joined: Sun Mar 14, 2021 3:37 am

Re: Is it possible to add a couple blog posts to homepage?

Post by nozaro33 »

I actually don't have any images to display using the JSON method, so I haven't tried it before. But if you have a url for you site / the issue I'll be happy to take a look.
OneManLaptop
Developer
Posts: 19
Joined: Tue Mar 16, 2021 3:29 pm

Re: Is it possible to add a couple blog posts to homepage?

Post by OneManLaptop »

nozaro33 wrote: Mon Mar 22, 2021 4:20 am I actually don't have any images to display using the JSON method, so I haven't tried it before. But if you have a url for you site / the issue I'll be happy to take a look.
I appreciate that. It's not online yet though and I haven't been able to replicate the problem since, so who knows what was happening. I'll keep an eye open though and I'll give you a shout if I need a second opinion. :)
Post Reply