dongyizhuang0134 2012-10-16 16:52
浏览 21
已采纳

显示与用户名匹配的类别中的帖子

This is fixed thanks to Alex's answer below.

So this is a bit of a weird one. I am setting up so I have category's with names "cat1, cat2, cat3" etc.

And say I had users called "cat1, cat2, cat3" so they matched the same as the category names.

I am then wanting to somehow only show the posts to the user that relates to their category, so basically if USER: "cat1" is logged in then they could only see the posts in category "cat1" as it matches their username.

I know you can do this to show posts only that the current user logged in has made but this wont work as the way the posts get put into the category complicated to explain.

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

?>

So if anyone has any insight into only showing posts to a user that matches a category with the same name as the user logged in hopefully they will be kind enough to help out.

  • 写回答

1条回答 默认 最新

  • doumaque6551 2012-10-16 18:25
    关注

    It seems like you've started a round-about way of getting this accomplished. Since you have categories already set up with the username, you should just be able to place that in your query

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

    EDIT:

    did you do var_dump($current_user->ID) to see if it matches the category name? That variable from the object might only contain an ID (like "1" or "178" etc). If that was the case, you'd need something like

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

    Then just replace the array with

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

    Alternatively, look at the Current User Functions (here). You may be able to use

    $current_user->user_login
    

    instead. You could also just try var_dump($current_user) (after calling global $current_user; get_currentuserinfo();) and see what variable contains the string you need

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退