Fun with PHP
I’ve been trying to figure out something, but I don’t really know PHP, so I’m having a bit of trouble with it. See, Wordpress innately doesn’t show private posts on the front page; you can only see them from within the admin panel. I just don’t really like that. Livejournal will display private posts if you’re logged in as the author, and I’m pretty sure this is doable in Wordpress too, I just need to figure out the code for it. Actually, what I want to do is specify a category and only display posts in that category if the user is currently logged in. It seems like that should only need a simple
if (!in_category(ID#) || $user-logged-in) check before the display code, but I’m not 100% confident in my programming logic skills anymore, plus I don’t know what the PHP syntax for such a statement, followed by a block of html code, would be. Plus I don’t know where to find $user-logged-in (since that’s a made-up variable). If said statement returns false, it’d need to skip the code that displays the post, and move on to the next iteration of the while loop. Does that even make sense? If you know or have any idea where a good resource might be (I’ve found a few references over at the WP forums, but they’re very old and not exactly what I’m looking for), please let me know~
Update: thanks to a look through some PHP books that sort of crystalized how PHP and HTML code work together, and those useful comments of cruinh’s, I’ve got this figured out. Hooray~
cruinh
May 2nd, 2005
5 years, 4 months ago
Well, here’s how you can tell if the user’s logged in. You may not need the first line if it’s already called earlier in the page. That line just gives you access to the $user_level variable.
get_currentuserinfo();
if ($user_level != 0)
echo "LOGGED IN";
else
echo "LOGGED OUT";
I’m still looking into how you would distinguish between private posts…
cruinh
May 2nd, 2005
5 years, 4 months ago
I dunno… it seems like the whole notion of Private Posts is just kinda broken in wordpress. I tried to find an explaination of them on the Codex and this is what I found: “Save as Private- This button prompts you to enter a password which will be required to view the post once it is posted live on your site. You will see the title of the post, but if anyone tries to view the article, they will be challenged to supply the proper password.”
Except, as you said, that’s not how it actually works. As far as I can tell marking a post as private is essentially the same as writing a draft. You can’t actually view private pages except perhaps in the admin tools. They don’t seem to be included in the query that pulls posts out of the database. Now if I could just find where that query is performed, maybe we could change it. So far it’s been elusive…
cruinh
May 2nd, 2005
5 years, 4 months ago
if there IS a way to include private posts in the query, this is what you’d need to change to make sure they’re only visible to people that’ve logged in. There’s a line in the Main Template file for each Theme that reads as:
if (have_posts()) : while (have_posts()) : the_post();
that would be changed to this.
if (have_posts()) : while (have_posts()) : the_post(); if ($user_level == 0 && $post->post_status == 'private'){ echo 'PRIVATE'; continue; }
cruinh
May 2nd, 2005
5 years, 4 months ago
except without the echo statement