L
L
ligisayan2015-10-12 12:57:27
PHP
ligisayan, 2015-10-12 12:57:27

Why can't check condition in functions.php file in wordpress?

Hello! I want to connect scripts for Yandex maps to work on certain wordpress pages using the functions.php configuration file. I write a condition like this:

if(is_home() || is_page(1825)) {
function yandex_api(){
    wp_enqueue_script( 'yandexmap', get_stylesheet_directory_uri() . '/js/ya-map.js');
    wp_enqueue_script( 'yandexapi', get_stylesheet_directory_uri() . '/js/yandex-maps.js');
}
add_action( 'wp_enqueue_scripts', 'yandex_api' );
}

but the map does not appear on the pages and the connection of scripts in the code is not displayed. if you remove the conditions - everything works .. What's wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Korolev, 2015-10-12
@ligisayan

Rewrite the function, the check should be done right inside it:

function yandex_api() {
    if ( is_home() || is_page( 1825 ) ) {
        wp_enqueue_script( 'yandexmap', get_stylesheet_directory_uri() . '/js/ya-map.js' );
        wp_enqueue_script( 'yandexapi', get_stylesheet_directory_uri() . '/js/yandex-maps.js' );
    }
}
add_action( 'wp_enqueue_scripts', 'yandex_api' );

A
Alexey Skobkin, 2015-10-12
@skobkin

While processing the functions.php file, it is not yet known what page it is.
Therefore, there is no point in doing such checks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question