Discover in this tutorial how to create a shortcode that allows you to display a list with the WordPress users you want.
In previous tutorials you have seen how to create a loop of users depending on their role or if they have an active subscription.
But it is possible that in some situation you want to display a list of users chosen or filtered by you.
So in this tutorial you are going to see how to create a shortcode that allows you to enter the identifiers of the users you want to display.
This would be the final result:
Steps to display a list of WordPress users according to their IDs
1. Create the shortcode
Add the following code snippet to your functionality plugin:
With this code you create a loop with the users that you specify in the shortcode
id parameter, showing the following elements:
- Image (avatar)
This is just an example. You can add or remove the fields you consider necessary using the function
In the loop arguments you indicate:
- The users you want to be displayed within the parameter
- To be sorted by name
You can modify the arguments you consider necessary.
2. Customize the CSS of the subscribers list
Now you just need to assign the styles to make it look the way you want.
To do this, insert this block at the end of style.css:
When creating the shortcode you have used the classes
user, which allow you to layout the listing as you wish.
With these few lines of CSS you can make it look like the example I showed you at the beginning. But, as always, I invite you to customize it to your liking.
3. Add the shortcode where you want to display the list of users
Finally, create the page where you want to display the list of subscribers and add the shortcode
[usuarios id="x,y,z"] where you replace the letters by the user identifiers. For example:
You can get the user ID by checking the URL when you edit it or, if you want to make it easier, get it displayed directly in the WordPress admin with this tutorial.
Now you know how to display a list with the WordPress users you want thanks to a shortcode with parameters.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂