code snippet for no-posts display message
-
Hi, I was wondering if I am even on the right track with this wordpress function. I want to put it in code-snippets so it exists across themes.
The issue is I am making a website for a client-friend. He wants a private blog that only registered users can view the posts of, and his homepage to display his latest posts. Since all posts are “private”, if the visitor is not logged in, no posts show, and with Illustratr, we get a “not found” message, which I edited to say “You are not logged in. Please log in to view all posts”. With Astra, a theme I want to switch to, we get a white screen in the middle (unless logged in, in which case we get an awesome display of posts). But this question is not about themes- because I already asked a more general question to the Astra forum. This question is about how to write a custom function/hook, and whether that is what I need to do. I worked out the following code, but it didn’t work out. Am I even on the right track?
function no_posts_on_home () { if ( is_home() && !have_posts() ){ return ?> <h1>You are not logged in. Please log in to view all posts.</h1> <?php ); } } add_action('pre_get_posts', 'no_posts_on_home ()');
Then I changed the return line to just be
echo “You are not logged in” ;I got this error:
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘no_posts_on_home ()’ not found or invalid function name in /app/public/wp-includes/class-wp-hook.php on line 286If there’s a better place to ask this, let me know!
Thanks for your help
- The topic ‘code snippet for no-posts display message’ is closed to new replies.