0

这要归功于 Alex 在下面的回答。

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

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

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

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

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

?>

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

4

1 回答 1

0

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

//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->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
);

或者,查看当前用户功能(此处)。您也许可以使用

$current_user->user_login

反而。您也可以尝试var_dump($current_user)(在调用之后global $current_user; get_currentuserinfo();)并查看包含您需要的字符串的变量

于 2012-10-16T18:25:19.180 回答