How do I add a function on a specific post category?

I am using theme Twenty Eleven and am trying to add a vote up script in the content.php which only displays on a certain category.

Related posts

Leave a Reply

1 comment

  1. you can do that in multiple ways ..
    For example you can do a conditional in the PHP itself with a wp built in function :

    if ( in_category( 'my-category' ) {
     //Do something
    }
    

    or for example, if the script is a javascript you could load the script in a conditional way , so it would not load where it is not needed – it will save also some processing time (change the condition to what you want).

        function dl_add_js_conditional() {
    
            if (!is_admin()) { //if is not admin - will load only on front-end
                wp_register_script('myscript_name_js', get_bloginfo('stylesheet_directory') . '/js/myscript_file.js', array('jquery'));
                wp_enqueue_script('myscript_name_js');
            }
    if (is_home()) { //will load only on home
                wp_register_script('myscript_name_js', get_bloginfo('stylesheet_directory') . '/js/myscript_file.js', array('jquery'));
                wp_enqueue_script('myscript_name_js');
            }
        }
        add_action('init', 'dl_add_js_conditional');
    

    Or , you can also do the detection / condition in the script itself (you did not specify what script you use, Jquery, Javascript or other)