0

I just added a new page which needed some javascript to pull json(with setInterval) and display content in that page div tag, On digging i discovered we can add script tag in themes/header.php file .

but that means it would load with all the pages or blog maybe, but I just want the javascript to load for that specific page.

function debug_to_console( $data ) {
    if ( is_array( $data ) )
        $output = "<script>console.log( 'Debug Objects: " . implode( ',', $data) . "' );</script>";
    else
        $output = "<script>console.log( 'Debug Objects: " . $data . "' );</script>";
    echo $output;
}

function loadJScriptforAho(){
        if  ( $post->ID == 75){
        debug_to_console( "Testing" );
        }
}
add_action( 'wp_enqueue_scripts', 'loadJScriptforAho' );

Above code added to functions.php but doesn't work

I'd imagine that might be possible but if someone can share how to that please let me know.

0

A simple google search can help you do this, well same question was answered before, you can see the answer here.

Use WordPress methods in your functions.php to do this :

function load_scripts() {
    global $post;
    wp_register_script( 'home', get_template_directory_uri() . '/js/home.js', array('jquery'));
    wp_register_script( 'about', get_template_directory_uri() . '/js/about.js', array('jquery'));
    wp_register_script( 'somepost', get_template_directory_uri() . '/js/somepost.js', array('jquery'));

    if( is_page() || is_single() )
    {
        switch($post->post_name) 
        {
            case 'home':
                wp_enqueue_script('home');
                break;
            case 'about':
                wp_enqueue_script('about');
                break;
            case 'some-post':
                wp_enqueue_script('somepost');
                break;
        }
    } 
}

add_action('wp_enqueue_scripts', 'load_scripts');
  • to test, I added the code posted in updated post to functions.php and nothing happened ! are you sure which php file am I supposed to add the function provided in solution to the link you shared ? – Ciasto piekarz Feb 7 '17 at 16:17
  • are you sure about the names? – Arsalan Mithani Feb 7 '17 at 16:22
0

You can do this a couple of ways, depending on how the page has been setup.

1) Page Template:

If you have a page template assigned to that specific page, you can use the following tags:

<?php if ( is_page_template('TEMPLATE FILE NAME') ) { ?>
    <!-- CONTENT GOES HERE -->
<?php } ?>

2) Page ID:

Like suggested above, you can do this by page ID - but I would do this slightly different:

<?php if ( is_page('ID OF PAGE GOES HERE') ) { ?>
    <!-- CONTENT GOES HERE -->
<?php } ?>

Let me know how you get on...

  • in which php module should I add ? – Ciasto piekarz Feb 7 '17 at 9:57
0

WordPress had hooks to achieve this.. Every page had a unique ID (post ID) this globally available in a page.

global $post;
If( $post->ID = 22 ) {
// Your code
}
  • this sits in which php page ? I meant by pages though not posts I hope you meant to edit page hooks ! – Ciasto piekarz Jan 29 '17 at 15:57
  • Use wp_head action hook.. in WordPress pages also refer posts ( posts and pages are stored in one table with different post_type ) if you need an example code please comment it.. i'll post an example code – Syed Abuthahir M Feb 7 '17 at 3:17
  • Yea please do, I am new to wordpress CMS and learning php with it, so an example would be great with div tag if condition is true for specific page – Ciasto piekarz Feb 7 '17 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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