Forum
Random Blog Post w/ comments section
Hi,
I am wanting to make a page that pulls in one random blog post out of all my blog posts.
I have been looking at the perch_blog_custom() documentation over at
https://docs.grabaperch.com/addons/blog/page-functions/custom/
but i cant seem to get a random blog post to show up.
I would also like the comments that are on that blog post to show with the new comment form.
What have you tried that isn't working?
Please post your code and Diagnostics Report with any request - it helps us to help you!
H Rachel,
The code i have so far is
The code is showing up the first post only, rather than showing a random article.
As for the comments and comments form, i literally have now idea how to get that to work. it isnt showing anything up.
my Diagnostics report is as follows
When you're using
perch_get('s')
do you mean to use the slug of the retrieved post?Im not really sure, thats the code in the example blog post to get comments. i suppose for that to work i need to be pulling in a slug which i current am not doing.
Sorry, Im new to Perch, 2nd site I've built using it (first with a blog) and Im more of a front end dev than a back end dev.
Hi, Thanks for that
But that didn't work, and now its not even showing up a blog post at all.
The above code shows one item. but it doesn't show a random item. Do i meed to pull in all my blogposts into an array and pick one at random. something a bit like this (this code doesn't actually work :-(. )
then where the echo is i render out the blog post Im after
What is output if you turn on debug?
Hi Rachel,
My debug output is
using the code
There was a typo:
Doh, how did I miss that.
Now I am just struggling to get the article to be random rather than the first blog post i posted.
Hi Jordan,
I think even for random you need to sort by something. So pick any id from your template and use that. E.g.:
Hope this helps
Cheers for all the help guys, but now there seems to be a different problem.
The comments are showing fine, but now im getting an new error when trying to load the actual post.
the code im using is
my debug is
line 96 is
Sorry to both everyone, it is very much appreciated.
What does this give you?
it now shows the post and comments section perfectly (random article each page view aswell)
but i'm getting this array wrapped around it
blog post here
there are no errors in my debug report
Oh, my mistake. Should be this:
You are all legends, thank you so much. Works perfect.
now which post do i mark as the solution, haha.