I have a PHP template file that requires some Visual Composer shortcode to be added. I only want the shortcode to display if a post belongs to certain categories. At any one time, at least three child categories will be chosen for the post.

How can I use echo do_shortcode to fire only when these categories are selected? What I want is a specific shortcode to be added to the content when the post is in a certain category. Ideally, it will get stripped when the user selects another category that isn't in the series. I'd add it as a div within the template file.

1 Answer 1


Sounds like you're looking for has_category(), which

Checks if the current post has any of given category.

You would use it, for example in single.php, like so,

<?php if ( has_category( array( 'cat-a', 'cat-b', 'cat-c' ) ) ) : ?>
        <?php echo do_shortcode( 'some_shortcode' ); ?>
<?php endif; ?>
  • superb - much appreciated Antti Sep 14, 2021 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.