Answer the question
In order to leave comments, you need to log in
How to load script in WP only on posts?
Dear craftsmen, tell me how you can load a js script exclusively on WordPress posts?
I understand that there is a vario to pull up along with all the scripts, but I would not want to. In the meantime, I have to be content with great things through posts and custom fields, but this is a so-so idea)
Answer the question
In order to leave comments, you need to log in
It's not entirely clear what you mean by "on the posts".. If this is the entry page itself, then through is_single(). If this is a general posts page, then one of the acceptable options is through a custom page template. I don’t know how universal this solution is, but it works)
my_custom_blog_page.php
<?
/*
* Template name: Страница записей
*/
// Код, приводимый далее можно скопировать с Вашей темы. Цикл вывода записей итд итп..
?>
<?
function if_blogpage(){
if(is_page_template('my_custom_blog_page.php')){
wp_enqueue_script('js_functions', '/* путь к вашему js */');
wp_localize_script( 'js_functions', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
}
}
// А вот когда активировать функцию проверки может решить только Ваша необъятная фантазия и необходимость. У меня для примера хедер
add_action('wp_head', 'if_blogpage');
// Данный код написан в качестве примера. Возможны правки и доработки
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question