H
H
Heorhii2018-06-25 18:36:36
css
Heorhii, 2018-06-25 18:36:36

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

2 answer(s)
A
Alexander Sobolev, 2018-06-25
@deasmont

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: Страница записей
 */
// Код, приводимый далее можно скопировать с Вашей темы. Цикл вывода записей итд итп..
?>

Now when you create the template use the js activation in the is_page_template() check
<?
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');
// Данный код написан в качестве примера. Возможны правки и доработки
?>

Y
yakupov112, 2018-06-25
@yakupov112

can be put in single.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question