0

With get_users() I am listing users and their information. I've a custom field in the database, people_lists_class.

<?php
    $blogusers = get_users('role=contributor&orderby=display_name');
    foreach ($blogusers as $user) {
        echo 
        '<li class="person member">' .

        '<a href="' . get_author_posts_url($user->ID, $author_nicename) . '">' .
        '<div class="user-thumbnail alignleft">' .
        get_avatar($user->ID, 50) .
        '</div>' .
        '</a>' .


        /* \/ \/ \/ Look at this: \/ \/ \/ */
        '<a href="' . get_author_posts_url($user->ID, $author_nicename) . '">' .
        '<h5 class="user-name user-info">' .
        $user->display_name . ', ' . get_user_meta($user->ID, 'people_lists_class', true) .
        '</h5>' .
        '</a>' .
        /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */

        '<p class="user-title user-info">' .
        get_user_meta($user->ID, 'people_lists_title', true) .
        '</p>' .

        '<p class="user-bio user-info">' .
        get_user_meta($user->ID, 'description', true) .
        '</p>' .

        '<a href="' . get_author_posts_url($user->ID, $author_nicename) . '">' .
        '<p class="user-permalink user-info">' .
        ' View articles by ' . $user->display_name .
        '</p>' .
        '</a>' .

        '<div class="clear"></div>' .
        '</li>';
    }
?>

To separate 'people_list_class' from 'display_name' I have a comma. But I only want the comma to show if the user have something written in the "people_lists_class"-field. Can I somehow integrate the following piece of code to accomplish this?

<?php if ( !empty( $user->people_lists_class ) ) {
    echo ', ';
}
else {
    echo '';
}
?>

1 Answer 1

0

When you have that much code to output concatenating it into one string like that makes it very difficult to read for others and yourself, also, to debug when things go wrong.

Try this method for concatenating your output,

$blogusers = get_users('role=contributor&orderby=display_name');
foreach ($blogusers as $user) {

    $name   = $user->display_name;
    $url    = get_author_posts_url($user->ID, $author_nicename);
    $class  = get_user_meta($user->ID, 'people_lists_class', true);
    $title  = get_user_meta($user->ID, 'people_lists_title', true);
    $desc   = get_user_meta($user->ID, 'people_lists_title', true);
    $avatar = get_avatar($user->ID, 50);

    $html  = '<li class="person member">';
    $html .= '<a href="' . $url . '"> <div class="user-thumbnail alignleft">' . $avatar . '</div></a>';
    $html .= '<a href="' . $url . '"><h5 class="user-name user-info">' $name . ($class ? ', ' . $class : '') . '</h5></a>';
    $html .= '<p class="user-title user-info">' . $title . '</p>';
    $html .= '<p class="user-bio user-info">' . $desc . '</p>';
    $html .= '<a href="' . $url . '"><p class="user-permalink user-info"> View articles by ' . $name . '</p></a>';
    $html .= '<div class="clear"></div></li>';

    echo $html;
}

So much nicer eh?

We store all of the information we need in variables that we pass along to our $html which means we can re-use the same variables over and over without making our code unnecessarily bloated with long verbose functions and function arguments to retrieve data. Not only that but its easier to read and if something goes wrong you'll find it much easier to debug your code than if you were doing it the other way around.

The particular statement which conditionally shows the comma ( , ) is;

 //shortened the line for brevity
 $name . ($class ? ', ' . $class : '') ... etc

So $name will show AND IF a value is held within people_lists_class it will print to screen the comma followed by whatever is in the class name field e.g. , class_name_here_bla_bla

In the end looking something like,

Bob Brown, Lowerclass Citizen

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.