Learn in this tutorial how to display only the entries of a category that a Restrict Content Pro subscriber has access to using a shortcode.
In a previous tutorial we saw how to display only the entries that a Restrict Content Pro subscriber has access to using a shortcode.
This time, you will be able to change not only the parameter that identifies the CPT of the posts you are going to show but also the number of posts and the category you want to show; and even show posts from several categories together.
Interesting, isn’t it?
This would be the final result for an entry type:
Do you want to see another one? Read on. 😉
Steps to display the entries of a given category to which an RCP subscriber has access using a shortcode
1. Restrict access to CPT entries by membership level
First, you need to restrict access to entries of the content type you want depending on the membership level.
To do this, just use the metabox that Restrict Content Pro creates at the end of the entries and select which membership level gives access to that content.
2. Generate the shortcode with parameters to show the posts of a category to which the subscriber has access
Add the following code at the end of functions.php or in your functionality plugin:
With this code you create a loop with the posts entries (
'post'), by default, or the CTP of your choice as we will see later.
The elements you display are (in order):
- Featured image (linked to the entry)
- Title (linked to the entry)
And you can modify:
- The sorting criteria (in this case by date)
- Descending order (in this case ASC, but you can change to DESC)
- Number of entries to display (in this case 12)
In addition, you add the
rcp_user_can_access conditional to only show the entry if the RCP subscriber has access to it.
You encapsulate all this inside a shortcode with
add_shortcode and add the parameter
'cpt', so you can tell it what type of entry you want it to display.
In this case, if you don’t pass any parameter to the shortcode, the default post type will be blog posts, but if you want it to be another one you only have to change it in:
$default_atts = array( 'cpt' => 'post' );
You can also customize to your liking the default number of posts which is set to 12 in the example.
You’re almost there.
3. Apply grid layout to the loop of posts (optional)
Add the following snippet to the end of the style.css file:
With this you get to display the posts in grid format. You can play with the minmax value (250px) to change the number of columns displayed.
Now there is only one step left.
4. Insert the shortcode with the parameter where you want it to go
The advantage of shortcodes is that you can insert them anywhere on the website.
You only have to type “[your_shortcode]” where you want it to be displayed
In this case, you can modify 3 parameters:
- The type of content
- The category
- The number of entries
For example, if you add this shortcode
[content_subscriber cats='frontpage' post='6'] it will show 6 posts from the frontpage category.
If instead, you use this shortcode:
[ it will show 12 entries of the tutorial CPT from the development and design categories.
content_subscriber cpt='tutorial' cats='development,design' post='12']
If the tutorial categories are a custom taxonomy you will have to change the line from
' taxonomy' => 'category' to
'taxonomy' => 'tutorial_category' or with the name with which you have registered the tutorial taxonomy.
In short, you just have to change the parameters of the shortcode you want. You can make as many combinations as you want.
If you want to display only the entries, from one or several categories, to which a Restrict Content Pro subscriber has access using a shortcode, now you know how to do it.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉