Monday, April 29, 2024
HomeWordPress DevelopmentShow customers by function - WordPress Improvement Stack Alternate

Show customers by function – WordPress Improvement Stack Alternate


What I perceive out of your code: You wish to checklist customers with creator function.

You are in the correct path, nevertheless, following are some tricks to make your code higher.

  • Use function id in lowercase syntax. Change Writer to creator for function property.
  • Get the outcomes for as soon as and keep away from repeated calls. Save the outcomes like $authors = $user_query->get_the_results(); then, use if (!empty($authors)) { ... } to verify and foreach($authors as $creator) {...} to loop by.
  • Wrap the precise leads to <ol> or <ul> or different wanted factor, so, begin the <ol> tag provided that outcomes are discovered and shut the </ol> tag proper after foreach loop. For the else content material, use different HTML factor equivalent to <p> with a error code fashion class and many others.
  • Think about pagination for prime variety of customers. Think about the case when the full matched customers go as much as 100+ to 1000s over the time.

After making use of the given suggestions, the code is as follows: –

With out pagination

<?php
$args = array(
    'function'    => 'creator', // modified Writer to creator
    'orderby' => 'user_nicename',
    'order'   => 'ASC'
);
$authors_query = new WP_User_Query($args); // enhanced the variable identify from $user_query to $authors_query 
$authors = $authors_query->get_results(); // saved the outcomes for as soon as to keep away from repeated calls in a while
?>

<?php if (!empty($authors)) { ?>
    <desk> <!-- Changed ol to desk factor -->
        <thead>
            <th>Title</th>
            <th>Particulars</th>
            <th>URL</th>
        </thead>
        <?php foreach ($authors as $consumer )  <?php echo $user->user_login ?>  //endforeach ?>
        </desk>
<?php } else { ?>
    <p class="no-results-notice">No authors discovered.</p>
<?php } ?>

With pagination

<?php

//Pagination
$web page = isset($_GET['show_authors']) ? $_GET['show_authors'] : 1;
$authors_per_page = 20;  // you'll be able to change the quantity as wanted
$total_pages = 1; // preliminary worth; hold it one; we'll replace this after having precise variety of customers from question
$offset = $authors_per_page * ($web page - 1);


$args = array(
    'function'    => 'creator', // modified Writer to creator
    'orderby' => 'user_nicename',
    'order'   => 'ASC'
);
$authors_query = new WP_User_Query($args); // enhanced the variable identify from $user_query to $authors_query 
$authors = $authors_query->get_results(); // saved the outcomes for as soon as to keep away from repeated calls in a while

// Updating variables for pagination.
$total_authors = $authors_query->get_total(); 
$total_pages = ceil($total_authors / $authors_per_page); 

?>

<?php if (!empty($authors)) { ?>
    <h2>We now have <?php echo $total_authors; ?> authors in our web site. </h2>
    <desk> <!-- Changed ol to desk factor -->
        <thead>
            <th>Title</th>
            <th>Particulars</th>
            <th>URL</th>
        </thead>
        <?php foreach ($authors as $consumer )  <?php echo $user->user_login ?>  //endforeach ?>
        </desk>

        <!-- Show Pagination Hyperlinks -->
        <div class="authors-pagination">
            <?php                 
                echo paginate_links( array(
                    'base'         => add_query_arg( 'show_authors', '%#%' ) ,
                    'complete'        => $total_pages,
                    'present'      => $web page,
                    'format'       => '',
                    'sort'         => 'plain',
                    'end_size'     => 1,
                    'mid_size'     => 1,
                    'prev_next'    => true,
                    'prev_text'    => sprintf( '<i></i> %1$s', __( 'Earlier', 'text-domain' ) ),
                    'next_text'    => sprintf( '%1$s <i></i>', __( 'Subsequent', 'text-domain' ) )
                ) );
            ?>
        </div>

<?php } else { ?>
    <p class="no-results-notice">No authors discovered.</p>
<?php } ?>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments