1

So, aside from creating individual WordPress Category PHP Pages, I'd much rather have a functions.php command that would insert shortcode when a Category has been selected.

How would I go about that, and is it indeed even possible?

Thanks!

0

Everything's possible, if you take the right approach.

What I've interpreted from your question is, that you want an specific shortcode to be added to your content when the post is in a special category. What you can do is to hook into the_content filter and add a shortcode, if the post is in a particular category.

add_filter( 'the_content', 'wpse314950_add_shortcode' );
function wpse314950_add_shortcode( $content ){
    global $post;

    // Check if this particular post has that category
    if ( has_category( 'my_category', $post ) ) {
        $content .= do_shortcode( '[some-shortcode arg_1="123" arg_2="xyz"]' );
    }

}

There might also be more approaches, such as:

  1. Adding a shortcode in the content automatically (using JS or jQuery) when the author chooses a category
  2. Adding the shortcode to every post and then stripping it out on other categories
  • Thanks for that....I was also thinking that a CSS approach might work within a div...so that I place the [SHORT CODE] in a div that is hidden if the category ID is not present...does that sound like a great plan or too basic? – henry Sep 24 '18 at 2:39
  • You're welcome! It can work. But two important questions: 1- How much does your shortcode add to page load? If it's a lot of contains image, not a good idea. 2- Does it contain sensitive information? If also true, again not a good idea. – Jack Johansson Sep 24 '18 at 3:08
  • Thanks Jack. Here's what I am trying to achieve: I have email capture forms that I'd like to display on certain categories. So, for example, if someone is viewing a post on say, for example, "apples" then a [SHORT CODE] would appear to encourage visitors to join the "fruit" newsletter. I hope that makes sense! Really the main point of doing this is to make my email marketing automation easier. – henry Sep 24 '18 at 3:18
  • That's fine then, you can use CSS. As long as you don't output heavy or sensitive content that could be manipulated, you'll be fine. – Jack Johansson Sep 24 '18 at 3:20

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.