【问题标题】:Show posts in category that match username在与用户名匹配的类别中显示帖子
【发布时间】:2012-10-16 16:52:17
【问题描述】:

感谢 Alex 在下面的回答,此问题已得到解决。

所以这有点奇怪。我正在设置,所以我有名称为“cat1、cat2、cat3”等的类别。

假设我的用户名为“cat1、cat2、cat3”,因此他们匹配的类别名称与类别名称相同。

然后我想以某种方式只向与他们的类别相关的用户显示帖子,所以基本上如果 USER: "cat1" 已登录,那么他们只能看到类别 "cat1" 中的帖子,因为它与他们的用户名匹配.

我知道您可以这样做以仅显示当前登录用户发布的帖子,但这不会起作用,因为帖子被放入难以解释的类别中。

<?php
// The Query
$args = array(
'post_status'=> '.....'
);
global $user_ID;
get_currentuserinfo();
if($user_ID) {
query_posts( $args,"author=$user_ID" );
}

?>

因此,如果有人对仅向与登录用户具有相同名称的类别匹配的用户显示帖子有任何见解,希望他们能够提供帮助。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您似乎已经开始了一种迂回的方式来完成这项工作。由于您已经使用用户名设置了类别,因此您应该能够将其放在查询中

    //Get User -> ID -> Cat
    global $current_user;
    get_currentuserinfo();
    $args = array(
        category_name => $current_user->ID
    );  //or use $current_user->display_name if that's how they are set up.
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
        //Display your loop how you want
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    

    编辑:

    您是否使用var_dump($current_user-&gt;ID) 来查看它是否与类别名称匹配?来自对象的变量可能只包含一个 ID(如“1”或“178”等)。如果是这样的话,你需要类似的东西

    $the_cat_name = 'category' . $current_user->ID;
    //Append your 'naming structure' to the front of the user ID
    

    然后只需将数组替换为

    $args = array( 
        category_name => $current_user->ID
    );
    

    或者,查看当前用户函数 (here)。你也许可以使用

    $current_user->user_login
    

    相反。你也可以试试var_dump($current_user)(在调用global $current_user; get_currentuserinfo();之后)看看哪个变量包含你需要的字符串

    【讨论】:

    • 这似乎正是我所需要的,但它无法正常工作,但我确实设置了相同的用户名和类别?
    • 你能看看这个吗,我已经添加了 php 代码和 var 转储。它似乎仍然没有显示任何内容?
    • 使用$current_user-&gt;user_login设法让它工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2015-12-07
    • 1970-01-01
    • 2017-07-08
    • 2017-01-19
    • 2019-09-21
    • 1970-01-01
    相关资源
    最近更新 更多