Forums

Compare custom field value with category name (23 posts)

  1. Marta86
    Member
    Posted 3 months ago #

    Hello!

    I want to do this:

    When a user login, it should appear the value of a profile custom field ("Fileira"), that can be the same as one of the categories' name.

    If the custom field value would be equal to a category's name, that value should become a link do that category.

    I've tried with this code, but it doesn't work:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    http://pastebin.com/yD5Uxqn9

    Can someone help me? Thanks!

  2. keesiemeijer
    moderator
    Posted 3 months ago #

    Try it with this [untested]: http://pastebin.com/GCpHm50F

  3. Marta86
    Member
    Posted 3 months ago #

    Thanks!! It worked :D

  4. Marta86
    Member
    Posted 3 months ago #

    Yet this is solved, now I have another problem.
    I'm in a category page and I want to compare the field "fileira" with the current category and, then, show all users with the value that is the same as the category.

    For example, I'm in category "Fish" page and I want to show the name of all users that have written "Fish" on "Fileira" profile custom field.

    I'm trying to solve this, but I don't find a solution.

    Can anyone help?

    Thanks!

  5. keesiemeijer
    moderator
    Posted 3 months ago #

    Not sure what you want but to get the current category ID you can use:

    $current_category =  get_query_var('cat');
    // to then get the name
    $current_category_name = get_cat_name( $current_category);
  6. Marta86
    Member
    Posted 3 months ago #

    That gave me the category name, but my problem isnt' solved yet :(

    I'm trying show all users' name and avatar that have the category name in fileira custom field value. I'm trying to compare category name with custom field value but something is wrong :(

    This is the code:

    function contributors() {
    global $wpdb;
    $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name"); //Go get all users
    
     $current_category =  get_query_var('cat');
    // to then get the cat name
    $current_category_name = get_cat_name( $current_category);
    
    foreach($authors as $author) { 
    
     $fileira = the_author_meta('fileira', $author->ID); //get the value of fileira custom field
    
    if(in_array($user_last, $current_category_name)) { //if the fileira custom field value is the same as category name
    //the users with current category ID/name on fileira custom field should appear
    
    echo "<li>"."<a href=\"".get_bloginfo('url')."/?author=".$author->ID."\">".get_avatar($author->ID)."</a>";
    echo '<div>'."<a href=\"".get_bloginfo('url')."/?author=".$author->ID."\">".the_author_meta('display_name', $author->ID)."</a>"."</div>"."</li>";
    echo '<div>'.$fileira."</div>"."</li>"; //show fileira value
    }
    
    else { //if it's empty
    //the users without the current category name should not appear	
    
    }
    
    }
    }
    
    contributors();
  7. Marta86
    Member
    Posted 3 months ago #

    What's wrong?

  8. keesiemeijer
    moderator
    Posted 3 months ago #

    $current_category_name is not an array, it's a string.
    $user_last is not defined in the function contributors();

    Maybe you want something like this:

    // convert both to lowercase and check if $fileira is the same as the current category
    if(strtolower($fileira) == strtolower($current_category_name)) { // rest of code
  9. Marta86
    Member
    Posted 3 months ago #

    But as category name as fileira custom field value usually have a capital letter. That code won't work, am I right?

    I've tried and it appears the value of all custom field with the else option (if it's not the same, it appears "no"):

    "VinhononoCereaisnoCarnenoVinhononoGestão e AvaliaçãonononoCereaisno"

    Sorry, it shouldn't be $user_last, but $fileira;

    function contributors() {
    global $wpdb;
    $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name"); //Go get all users
    
     $current_category =  get_query_var('cat');
    // to then get the cat name
    $current_category_name = get_cat_name( $current_category);
    
    foreach($authors as $author) { 
    
     $fileira = the_author_meta('fileira', $author->ID); //get the value of fileira custom field
    
    if(strtolower($fileira) == strtolower($current_category_name)) {
     //if the fileira custom field value is the same as category name
    //the users with current category ID/name on fileira custom field should appear
    
    echo "<li>"."<a href=\"".get_bloginfo('url')."/?author=".$author->ID."\">".get_avatar($author->ID)."</a>";
    echo '<div>'."<a href=\"".get_bloginfo('url')."/?author=".$author->ID."\">".the_author_meta('display_name', $author->ID)."</a>"."</div>"."</li>";
    echo '<div>'.$fileira."</div>"."</li>"; //show fileira value
    }
    
    else { //if it's empty
    //the users without the current category name should not appear
    echo "no";
    }
    
    }
    }
    
    contributors();
  10. keesiemeijer
    moderator
    Posted 3 months ago #

    That code won't work, am I right?

    I only use that to check with both values to lowercase. If a user typed "fish" instead of "Fish" the conditional if($fileira == $current_category_name) would be false. The variable $fileira or $current_category_name is never set to lowercase.

    try changing:

    $fileira = the_author_meta('fileira', $author->ID); //get the value of fileira custom field

    to

    $fileira = get_the_author_meta('fileira', $author->ID); //get the value of fileira custom field
  11. Marta86
    Member
    Posted 3 months ago #

    Problem persists :(

    If I write echo $fileira; and echo $$current_category_name, the first shows all values in that field and the second show the right category name.

    But when I want to compare, it jumps to the else "echo 'no'";

    So the comparison is wrong, but I don't know the right code :/

    When I put the code inside the if loop, outside it,

    $fileira = get_the_author_meta('fileira', $author->ID); //get the value of fileira custom field
    
    echo "<li>"."<a href=\"".get_bloginfo('url')."/?author=".$author->ID."\">".get_avatar($author->ID)."</a>";
    echo '<div>'."<a href=\"".get_bloginfo('url')."/?author=".$author->ID."\">".the_author_meta('display_name', $author->ID)."</a>"."</div>"."</li>";
    echo '<div>'.$fileira."</div>"."</li>";

    I get all the users with the avatar, name and the fileira field value, but the "no" of the else is always showing up.

    else { //if it's empty
    echo "no";
    }
  12. keesiemeijer
    moderator
    Posted 3 months ago #

    the first shows all values in that field

    Has it more values than one? Or is it just a string?

    Can you show the values.

  13. Marta86
    Member
    Posted 3 months ago #

    The Fileira custom field is a text field

    <tr>
    	<th><label for="fileira"><?php _e('Fileira'); ?></label></th>
    	<td><input type="text" name="fileira" id="fileira" value="<?php echo esc_attr($profileuser->fileira) ?>" class="regular-text" /></td>
    </tr>

    So the user can write whatever they want.

    The categories are: Fish, Meat, Wine, Cereals, Honey, Vegetables, Olive Oil and Milk.

    So what I want is if a user is on a category page (eg. Fish), he/she can see all users that "belong" to that category, by getting the value of Fileira custom field and comparing it with the category name of the current page and, if it's the same, appears avatar and name of those users.

    I get all users, than the custom field value and the current category, but when I'm comparing them, the if doesn't work.

  14. keesiemeijer
    moderator
    Posted 3 months ago #

    I've tested it with this in my functions.php: http://pastebin.com/mnEhUBQ6
    and this on a category.php: http://pastebin.com/8FciHn6A

    And it works.

  15. Marta86
    Member
    Posted 3 months ago #

    Oh my god, at first glance, it works!! :)

    Thank you so much :D

    The problem was on creating the custom field?

  16. keesiemeijer
    moderator
    Posted 3 months ago #

    Glad it works. But why not create radio buttons for the categories? That way you are sure it is an existing category in the fileira author_meta

  17. Marta86
    Member
    Posted 3 months ago #

    I've been thinking about that, but I'm not the boss, I must talk first with the company :)

  18. Marta86
    Member
    Posted 3 months ago #

    By the way, as you're so kind, can you help with one more thing?

    I want to create an else that shows "no Members yet" if there's no user with the conditions defined on the if loop.

    I've written:

    if(strtolower($fileira) == strtolower($current_category_name)) {
     //if the fileira custom field value is the same as category name
    //the users with current category ID/name on fileira custom field should appear
    
    //echo "<li>"."<a href=\"".get_bloginfo('url')."/?author=".$author->ID."\">".get_avatar($author->ID)."</a>";
    echo "<ul><a href=\"".get_bloginfo('url')."/?author=".$author->ID."\">";
    $nomi = the_author_meta('display_name', $author->ID);
    echo "</a></ul>";
    }

    And now I want an else:

    else {
    echo "no members yet";
    }

    Is there a reason to, with this code, the "no members" being shown, despite of having users to show?

  19. keesiemeijer
    moderator
    Posted 3 months ago #

    It loops through all the authors even the one's that don't have no members yet. In stead of echoing you could put it in a variable and echo it after the author foreach:

    $html = '';
    foreach($authors as $author) {
    $fileira = get_the_author_meta('fileira', $author->ID);
    if(strtolower($fileira) == strtolower($current_category_name)) {
     //if the fileira custom field value is the same as category name
    //the users with current category ID/name on fileira custom field should appear
    
    //echo "<li>"."<a href=\"".get_bloginfo('url')."/?author=".$author->ID."\">".get_avatar($author->ID)."</a>";
    $html .= "<ul><a href=\"".get_bloginfo('url')."/?author=".$author->ID."\">";
    $html .= the_author_meta('display_name', $author->ID);
    $html .= "</a></ul>";
    }
    } // end author foreach  
    
    if($html != '') {
    echo $html;
    } else {
    echo "no members yet";
    }
  20. Marta86
    Member
    Posted 3 months ago #

    That works :) But for some reason, the link to author page doesn't work when I put into the variable.

  21. keesiemeijer
    moderator
    Posted 3 months ago #

    Try it with:

    $html .= get_the_author_meta('display_name', $author->ID);
  22. Marta86
    Member
    Posted 3 months ago #

    Now it's perfect! Thank you so much! You saved my life! :)

  23. keesiemeijer
    moderator
    Posted 3 months ago #

    No problem. I'm glad you got it resolved.

Reply

You must log in to post.

About this Topic