WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to Get User Profile Info on a Category Page (6 posts)

  1. trivum
    Member
    Posted 2 years ago #

    I'd like to pull in profile info (name, web address, etc. + some custom-made fields) for specific users onto some of my category pages.

    I realize I can pull in profile info using "current author," but this won't help in this case because the user won't be associated with the category page as an author.

    For example, I'd like to be able to do the following.

    On the Category A page I'd like to put User-John's profile information (but User-John isn't an author for any of the posts in Category A).

    On the Category B page I'd like to put User-Bob's profile information (but User-Bob isn't an author for any of the posts in Category B).

    But I will need to do this for hundreds or thousands, so I need a at least a somewhat automated way. I thought of using the Category Description box, but it doesn't seem to accept PHP (I've gotten it to accept HTML and even shortcodes).

    Any ideas? ... Thanks in advance.

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with one of these functions:

    http://codex.wordpress.org/Function_Reference/get_userdata
    http://codex.wordpress.org/Function_Reference/get_the_author_meta

    What is the relationship between the category and the user?

  3. trivum
    Member
    Posted 2 years ago #

    Thanks for the reply.

    The problem is there isn't a relationship between the category and the user (not that WordPess knows about anyway - this is on purpose).

    Also, because I will need to do this on a large scale, I need a somewhat automated way to do it. Therefore, in order to do something like this:

    <?php $user_info = get_userdata(1);
          echo 'Username: ' . $user_info->user_login . "\n";
          echo 'User level: ' . $user_info->user_level . "\n";
          echo 'User ID: ' . $user_info->ID . "\n";
    ?>

    I need to be able to easily and dynamically change the user ID above depending on the category, so I can put, for example, the user ID 1 for Category A and the user ID 2 for Category B, etc.

    As I mentioned, I thought of using the category description box and then somehow working with the code that calls that:

    <?php echo category_description( $category_id ); ?>

    I thought maybe that I could go to the category description box for Category A and put in a 1 (for user ID #1) and then go to Category B and put in a 2 (for user ID #2), etc.

    But I can't figure out how to get that user ID number to print in the right place, i.e. in the $userID section of

    get_userdata($userID);

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    On a category page (category.php) you can use this:

    $object = get_queried_object();
    if($object->description) {
    $user_info = get_userdata($object->description);
    
    // rest of code
    
    }
  5. trivum
    Member
    Posted 2 years ago #

    Excellent! That's perfect.

    Thanks a lot, keesiemeijer. You've helped me solve a big piece of my puzzle.

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    No problem. Glad you got it resolved.
    category_description() will return the description in a html paragraph tag (just found that out).
    That's why this wouldn't work:

    $user_info = get_userdata(category_description());

    but this would:

    $desc = (int) strip_tags(category_description());
    if($desc){
    $user_info = get_userdata($desc);
    
    // rest of code
    
    }

Topic Closed

This topic has been closed to new replies.

About this Topic