简体   繁体   English

如果否则 WP is_user_logged_in 没有显示正确的内容

[英]if else WP is_user_logged_in doesn't show correct content

I just write following code我只写下面的代码

if ( is_user_logged_in() ) 
{
    $filter = array(
        'post_status' => 'private',
    );
} 
else 
{
    $filter = array(
        'post_status' => 'publish',
    );
}

I have header with token so should it work.我有带令牌的标头,所以它应该工作。 But always appears 'publish' posts但总是出现“发布”帖子

What I'm doing wrong?我做错了什么?

Welcome to Stack Overflow!欢迎使用堆栈溢出! It looks like you've got a few issues.看起来您遇到了一些问题。 First and foremost, make sure your code is properly formatted with indents/spaces.首先,确保您的代码使用缩进/空格正确格式化。 It gets very hard to read very quickly without having a consistent formatting structure:它变得非常难,而不必一致的格式化结构非常快速阅读:

正确缩进和格式化的代码的屏幕截图

With that said, it seems like you've got a ways to go on understanding the fundamentals of WordPress programming, and/or you're not including all of the code.话虽如此,您似乎有办法继续了解 WordPress 编程的基础知识,并且/或者您没有包含所有代码。 For instance, what does $filter reference, when/where is this code being run?例如, $filter引用了什么,这段代码何时/何地运行? You're either missing the surrounding code in your example, or you just put this code somewhere and there's nothing for it to reference and modify.您要么在示例中缺少周围的代码,要么只是将此代码放在某处而没有任何内容可供参考和修改。

WordPress makes use of a Template Hierarchy and is extensible via Filter Hooks and Action Hooks . WordPress 利用模板层次结构,并且可以通过过滤器钩子动作钩子进行扩展。 You'll need at least a general understanding of those, as well as "The Loop" , in order to really get WordPress to do what you want.您至少需要对这些以及“循环”有一个大致的了解,才能真正让 WordPress 做您想做的事。

Based on the snippet you've provided, it looks like you want to display only private posts to users that are logged in, I'm assuming in your main query.根据您提供的代码段,您似乎只想向登录的用户显示私人帖子,我假设在您的主查询中。 You can make use of the pre_get_posts hook to modify a WP Loop query.您可以使用pre_get_posts挂钩来修改 WP Loop 查询。 Something like this should get you started:像这样的事情应该让你开始:

add_action( 'pre_get_posts', 'modify_query_if_logged_in' );
function modify_query_if_logged_in( $query ){
    // Abort if we're in /wp-admin or this isn't the main query
    if( ! is_admin() && $query->is_main_query() ){
        // If user is logged in
        if( is_user_logged_in() ){
            // Only show private posts
            $query->set( 'post_status', 'private' );
        }
    }
}

Just drop that in your functions.php file and it should work.只需将它放在您的functions.php文件中,它应该可以工作。 You can also add in any other conditions you want, like is_front_page() to only have it run on the home page (which sort of makes ! is_admin() redundant and unnecessary), or adjust the $query to show both regular and private posts with $query->set( 'post_status', array('private', 'publish') );您还可以添加任何其他您想要的条件,例如is_front_page()使其仅在主页上运行(这使得! is_admin()变得多余和不必要),或调整$query以显示常规私人帖子使用$query->set( 'post_status', array('private', 'publish') );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM