How to Use Custom Shortcodes in Yoast SEO Title and Yoast Meta Description
In this WordPress tutorial, we’ll explore a powerful technique to boost your website’s SEO title and meta description by using custom shortcodes. Shortcodes are versatile WordPress features that allow you to dynamically insert content into different parts of your website, providing engaging and dynamic information to both users and search engines. We will create a custom shortcode that displays the total count of published posts for a specific custom post type, excluding certain categories, and we’ll utilize this shortcode in both the Yoast SEO title and the Yoast meta description.
Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.
Create the Custom Shortcode Function
To begin, we’ll create a basic example of a custom shortcode function that calculates the total number of published posts for a specific custom post type while excluding certain categories. Please note that this function is a basic setup and can be further enhanced and customized to fit your specific needs and requirements.
Open your theme’s functions.php
file using a text editor or the WordPress theme editor and add the following code snippet to the functions.php
file:
function custom_post_type_count_shortcode() {
$args = array(
'post_type' => 'my_post_type_slug', // Replace 'my_post_type_slug' with your actual custom post type slug.
'post_status' => 'publish',
'category__not_in' => array( 27, 124 ), // Replace with category IDs you want to exclude.
);
$post_ids = get_posts( $args );
$post_count = count( $post_ids );
return $post_count;
}
add_shortcode( 'custom_post_type_count', 'custom_post_type_count_shortcode' );
Code language: PHP (php)
Explanation:
- We created a function called
custom_post_type_count_shortcode
. - The
$args
variable is set to retrieve the post count for your specified custom post type while excluding specific categories. - We use
get_posts()
to fetch posts based on the provided arguments. - The
count()
function is used to calculate the total number of published posts. - Return the post count.
Filter the Yoast SEO Title and Meta Description
Next, we’ll set up a single function to replace the [custom_post_type_count]
shortcode with the actual post count in both the Yoast SEO title and the Yoast meta description. This process will dynamically update the SEO information based on the calculated post count provided by our custom shortcode function.
To achieve this, we’ll use WordPress filters, which allow us to modify the content before it is displayed. The filters we’ll be using are specifically designed for the Yoast SEO plugin, making it seamless to incorporate the shortcode within the SEO title and meta description.
Still in the functions.php
file, add the following code snippet below the previous one:
function replace_post_count_placeholder( $content ) {
// Check if the content contains the [custom_post_type_count] shortcode placeholder
if ( strpos( $content, '[custom_post_type_count]' ) !== false ) {
// Get the post count using the shortcode function
$post_count = custom_post_type_count_shortcode();
// Replace the [custom_post_type_count] shortcode with the actual post count
$content = str_replace( '[custom_post_type_count]', $post_count, $content );
}
return $content;
}
add_filter( 'wpseo_title', 'replace_post_count_placeholder' );
add_filter( 'wpseo_metadesc', 'replace_post_count_placeholder' );
Code language: PHP (php)
Explanation:
- We created a function named
replace_post_count_placeholder
. - The function checks if the content (either the Yoast SEO title or meta description) contains the
[custom_post_type_count]
shortcode. - If the shortcode is found, we call the
custom_post_type_count_shortcode()
function (created in Step 1) to retrieve the post count. - We use
str_replace()
to replace[custom_post_type_count]
with the actual post count in the content. - The function then returns the modified content.
By adding these filters, WordPress will automatically apply the replace_post_count_placeholder
function to the Yoast SEO title and meta description content. As a result, whenever you create or update a page, the shortcode [custom_post_type_count]
will be dynamically replaced with the actual total number of published posts for your specified custom post type, excluding the designated categories.
Using the Shortcode in Yoast SEO Title and Meta Description
Now that the custom shortcode function is ready and the filters are set up, you can incorporate the [custom_post_type_count]
shortcode into your Yoast SEO title and meta description in the backend of your website.
For Yoast SEO Title:
Explore Our Collection of [custom_post_type_count] Books - YourWebsiteName
Code language: PHP (php)
or Yoast Meta Description:
Discover our collection of [custom_post_type_count] books and level up your knowledge.
Code language: PHP (php)
When Yoast generates the title and meta description for a page, it will execute the corresponding filters (replace_post_count_placeholder
) to dynamically replace [custom_post_type_count]
with the actual total number of published posts for the specified custom post type.
Remember to replace “my_post_type_slug
” with the actual slug of your custom post type and “YourWebsiteName
” with your website’s name.
That’s it! You’ve successfully implemented a basic example of a custom shortcode function to display the total count of published posts for a specific custom post type while excluding certain categories in both the Yoast SEO title and the Yoast meta description.
Keep in mind that the provided custom shortcode function is just a starting point, and you can further enhance it to suit your specific needs. You may modify the function to consider additional parameters, customize the output format, or include more complex logic depending on your website’s requirements.
Now, your website will automatically display an SEO title and meta description that dynamically reflects the total count of published posts for your custom post type, excluding specific categories as desired. As you create new posts or update existing ones, the SEO information will adjust accordingly, providing valuable insights to users and enhancing your website’s SEO efforts.
Leave your feedback and help us improve 🐶
We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.
Your thoughts matter, leave a reply 💬