Discover in this tutorial how to display a number of posts from a Custom Post Type (CPT) that is automatically updated using a custom shortcode.
Following a subscriber request, I prepared a tutorial to display a dynamic number of posts that is always kept updated in the Genesis Sample home page.
In that article you added the code snippet in the front-page.php template.
But what happens if you want to insert that text in any other place of your website (inside a post, in a widget….)?
In that case you can use another strategy: creating a custom shortcode.
Here we go.
Instructions to generate a shortcode that shows the number of entries always updated
1. Create the shorcodes
Imagine you want to get the same text as in the tutorial where you entered variable numbers in the Genesis Sample home page:
Take advantage of more than X courses and Z tutorials.
To do this add the following code at the end of functions.php:
To see this and another 965 code snippets of this website, login or subscribe here.
These snippets show the number of entries of the CPT “Courses” and the number of normal entries (in this case tutorials).
If you look at the code, you will see that you are subtracting ‘1’ from the number of entries, so that you can say “more than…” with total rigour (note here my scientific background, ;)) and you add the words ‘courses’ and ‘tutorials’ to avoid having to add them later.
This way, to get the previous sentence you would have to write:
“Take advantage of the more than [number_courses] and [number_posts].”
This is just an example, but you can adapt it to your taste.
For example:
2. Customize the shortcode
Go one step further and customize the shortcode to your needs.
Suppose you want to create a list that indicates the number of various types of entries:
- Courses: [number_courses].
- Tutorials: [number_posts].
- …
In that case you would remove the subtraction (the ‘-1’) and the word you had added afterwards (since it already goes before).
The snippet would be like this:
To see this and another 965 code snippets of this website, login or subscribe here.
3. Enter the shortcode wherever you want
You see that the concept is very similar to the tutorial with which you get to display these variable numbers in the home of Genesis Sample, but with the advantage that you can insert them anywhere on the web thanks to the shortcode.
You only have to write “[your_shortcode]” where you want it to be displayed.
Important:
- If you are using the classic editor, use the HTML tab to introduce the shortcode.
- In case you are already using the block editor you can add it to the normal paragraph block, the HTML block or the shortcode block. All of them will render the content correctly.
Conclusions
Now you know how to insert a number of courses (or any other type of entry) that will always be kept up to date anywhere on the web using a shortcode.
Also, you’ve seen that with small variations you can get it customized to fit your needs.
All you have to do now is to put it into practice. 😉
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. 😉