• 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!

Viewing 15 replies - 1 through 15 (of 22 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

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

    Thread Starter Marta86

    (@marta86)

    Thanks!! It worked 😀

    Thread Starter Marta86

    (@marta86)

    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!

    Moderator keesiemeijer

    (@keesiemeijer)

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

    Thread Starter Marta86

    (@marta86)

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

    Thread Starter Marta86

    (@marta86)

    What’s wrong?

    Moderator keesiemeijer

    (@keesiemeijer)

    $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

    Thread Starter Marta86

    (@marta86)

    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();
    Moderator keesiemeijer

    (@keesiemeijer)

    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

    Thread Starter Marta86

    (@marta86)

    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";
    }

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter Marta86

    (@marta86)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter Marta86

    (@marta86)

    Oh my god, at first glance, it works!! 🙂

    Thank you so much 😀

    The problem was on creating the custom field?

    Moderator keesiemeijer

    (@keesiemeijer)

    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

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘Compare custom field value with category name’ is closed to new replies.