1

I'm creating a shortcode to display the list of users with a particular profile in WordPress.

But I can not enable or display the pagination. I have 25 users on my site and would like to display 5 profile users per page and include the pagination.

I added a new Role student, and multiple custom fields using Advanced Custom Fields for the student profile.

So I need to display a list of students as the appropriate fields. So far so good, the problem is with paging that I could not enable.

Here a example of my code:

add_shortcode('list-users', 'list_users');

function list_users($atts){

global $wpdb;

$args = array(
    'blog_id'      => '',
    'role'         => 'student',
    'meta_key'     => 'pw_user_status', //I'm using the New User Approve Plugin
    'meta_value'   => 'approved',//If user is denied, he is not appears in the list
    'meta_compare' => '',
    'meta_query'   => array(),
    'include'      => array(),
    'exclude'      => array(),
    'orderby'      => 'display_name',
    'order'        => 'ASC',
    'offset'       => '',
    'search'       => '',
    'number'       => '2',
    'count_total'  => true,
    'fields'       => 'all',
    'who'          => ''
);

$students = get_users($args);

if(is_user_logged_in()){

$content = "<div id='list-of-users'>";

   foreach($students as $student){

    $content .= "<li>";
    $content .= "<div class=\"edit-profile\">";
    if(current_user_can('administrator')){
       $link_of_profile_of_user = get_edit_user_link($aluno->ID);
       $content .= "<a href=".$link_of_profile_of_user." class=\"edit-profile\">"."Edit this profile"."</a>";
     }
    $content .= "</div>";
    $content .= get_avatar($student->ID);
    $content .= "<span class='item'>Name: </span>" . get_the_author_meta('display_name', $student->ID) . "<br />";
    $content .= "<span class='item'>Date of Birth: </span>" . get_the_author_meta('date_of_birth_acf_student', $student->ID) . "<br />";
    $content .= "<span class='item'>Course: </span>" . get_the_author_meta('course_acf_student', $aluno->ID) . "<br />"
    $content .= "<span class='item'>Email: </span>" . get_the_author_meta('user_email', $student->ID) . "<br />";
    $content .= "<span class='item'>Phone: </span>" . get_the_author_meta('phone_acf_student', $student->ID) . "<br />";
    $content .= "<span class='item'>City: </span>" . get_the_author_meta('city_acf_student', $student->ID) . "<br />";
    $content .= "</li>";

   }//close foreach

$content .= "</div>";

    return $content;

}//close the conditional for user logged

}//close function
0

2 Answers 2

0

I realise that you say that you can't enable paging of users, but if it's a case of you don't know how, you can pass 'number' and 'offset' in the args array.

In your example to get page 2 of your users, set 'number' as '5' and 'offset' as '5'.

If using those variables is not suitable you can always use a for loop with a count (instead of foreach) and insert a break every 5 iterations eg.

for ($i = 0; $i < count($students); $i++) {
    // some code here
    if ($i % 5 == 0 && $i != 0 && $i != count($students)) {
        // insert page break code here
    }
}
1
  • Hooo... Sorry, I did not get an email notification about this reply. I apologize for not replying at time. I ended up using a plugin to do the job. The plugin is: Members List. Thank you.
    – Enoque
    May 23, 2013 at 3:11
0

I don't use pages in WordPress posts or pages too often, so I first did a test with this hand written markup. I used 26 users to purposefully get a last page with less than 5 students on it. Just to be certain.

<ul>
    <li>A</li>
    <li>B</li>
    <li>C</li>
    <li>D</li>
    <li>E</li>
</ul>

<!--nextpage-->

<ul>
    <li>F</li>
    <li>G</li>
    <li>H</li>
    <li>I</li>
    <li>J</li>
</ul>

<!--nextpage-->

<ul>
    <li>K</li>
    <li>L</li>
    <li>M</li>
    <li>N</li>
    <li>O</li>
</ul>

<!--nextpage-->

<ul>
    <li>P</li>
    <li>Q</li>
    <li>R</li>
    <li>S</li>
    <li>T</li>
</ul>

<!--nextpage-->

<ul>
    <li>U</li>
    <li>V</li>
    <li>W</li>
    <li>X</li>
    <li>Y</li>
</ul>

<!--nextpage-->

<ul>
    <li>Z</li>
</ul>

WordPress produces 6 pages with links in both the Page and the Post editor. You didn't specify which you were using.


To produce this markup with PHP, I created list of dummy students (A-Z) with this code since I could not produce your list.

// Change this to change the number of students per page.
$students_per_page = 5;

// Set up a dummy user list. (26)
$students = str_split( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' );

// Break the whole list in page sized chunks.
$student_chunks = array_chunk( $students, $students_per_page );

// Cycle through each page.
foreach ( $student_chunks as $student_chunk )
    $pages[] = wpse_98352_get_student_page( $student_chunk );

// Add page breaks and echo post or page.
echo join( "\n<!--nextpage-->\n", $pages );

/**
 * This code should be replaced with the actual code for each students
 * page.
 *
 * @param array $students A page worth of students.
 * @return string. Text of for this page.
 */
function wpse_98352_get_student_page( $students ) {

    $return .= '<ul>';

    foreach ( $students as $student )
        $return .= "<li>$student</li>";

    $return .= '</ul>';

    return $return;
}

I got this:

<ul><li>A</li><li>B</li><li>C</li><li>D</li><li>E</li></ul>
<!--nextpage-->
<ul><li>F</li><li>G</li><li>H</li><li>I</li><li>J</li></ul>
<!--nextpage-->
<ul><li>K</li><li>L</li><li>M</li><li>N</li><li>O</li></ul>
<!--nextpage-->
<ul><li>P</li><li>Q</li><li>R</li><li>S</li><li>T</li></ul>
<!--nextpage-->
<ul><li>U</li><li>V</li><li>W</li><li>X</li><li>Y</li></ul>
<!--nextpage-->
<ul><li>Z</li></ul>

Which I pasted into a page to test.


Then I adjusted the $students_per_page variable to 6, which got me this:

<ul><li>A</li><li>B</li><li>C</li><li>D</li><li>E</li><li>F</li></ul>
<!--nextpage-->
<ul><li>G</li><li>H</li><li>I</li><li>J</li><li>K</li><li>L</li></ul>
<!--nextpage-->
<ul><li>M</li><li>N</li><li>O</li><li>P</li><li>Q</li><li>R</li></ul>
<!--nextpage-->
<ul><li>S</li><li>T</li><li>U</li><li>V</li><li>W</li><li>X</li></ul>
<!--nextpage-->
<ul><li>Y</li><li>Z</li></ul>

So, it looks like the algorithm works on different students per page, even when the final page is less than a full page.

NOTE: You will need to change the wpse_98352_get_student_page() function to produce your page data. I just used this markup to make testing possible. You will also have to stuff all this into the shortcode function.

1
  • Hooo... Sorry, I did not get an email notification about this reply. I apologize for not replying at time. I ended up using a plugin to do the job. The plugin is: Members List. Thank you.
    – Enoque
    May 23, 2013 at 3:10

Your Answer

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

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