1

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.

1

1 Answer 1

2

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)

1
  • Could this be done in functions.php ?
    – Drewdavid
    Commented May 7, 2021 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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