How to append shortcode after content in category in WordPress

To append a shortcode after the content of a post that is in a specific category in WordPress, you can use the the_content filter hook in your theme’s functions.php file. This hook allows you to modify the content of a post before it is displayed on the front-end of your site.

Here’s an example of how you can use this hook to append a shortcode after the content of a post that is in a specific category:

// Append shortcode after content in category
// More snippets at wpunplugged.com 

add_filter( 'the_content', 'append_shortcode_in_category' );
function append_shortcode_in_category( $content ) {
 
    // Check if the post is in a specific category
    if ( in_category( 'category-slug' ) ) {
 
        // Append the shortcode to the content
        $content .= do_shortcode( '[your-shortcode]' );
    }
 
    // Return the modified content
    return $content;
}

In this example, you will need to replace “category-slug” with the slug of the category you want to target, and “[your-shortcode]” with the actual shortcode you want to append to the content.

Keep in mind that this code will only work if you have a shortcode function defined in your theme or a plugin.

Share this post

You might also like...

Comment

Your email address will not be published. Required fields are marked *