Run a function on a WP page template – is_page_template

I am trying to run a function on a specific WP page template. The specific page is called archive.php.

This is what I have so far in functions.php

Read More
if ( is_page_template( 'bloginfo("stylesheet_directory")/archive.php' ) ) {
    function add_isotope() {
        wp_register_script( 'isotope', get_template_directory_uri().'/js/isotope.pkgd.min.js', array('jquery'),  true );
        wp_register_script( 'isotope-init', get_template_directory_uri().'/js/isotope-hideall.js', array('jquery', 'isotope'),  true );
        wp_enqueue_script('isotope-init');
    }

    add_action( 'wp_enqueue_scripts', 'add_isotope' );
} else {
    function add_isotope() {
        wp_register_script( 'isotope', get_template_directory_uri().'/js/isotope.pkgd.min.js', array('jquery'),  true );
        wp_register_script( 'isotope-init', get_template_directory_uri().'/js/isotope.js', array('jquery', 'isotope'),  true );
        wp_enqueue_script('isotope-init');
    }

    add_action( 'wp_enqueue_scripts', 'add_isotope' );
}

The different between the functions is isotope-hideall, which hides all categories when the page is loaded. When not using if/else it hides all categories from all page templates when page is loaded, and that is not what I want. Therefor I am using if/else to locate the correct page template.

I have tried the following, but nothing seems to work:

is_page_template( 'archive.php' )

is_page_template( 'get_template_directory_uri()'.'/archive.php' )

Am I doing something wrong, or do you have a working solution for this?

Page can be found here.

Related posts

2 comments

  1. As Pieter Goosen points out, archive.php is reserved for built in WordPress-functionality. Rename your file to something else, for instance to archives.php and make sure you are naming your custom page template at det top of the file:

    <?php /* Template Name: Archives */ ?>
    

    Your code should then work with is_page_template('archives.php') as longs as its located on root in your template folder. If not add whatever folder structure you have in front of the filename, like so: /folder/folder2/archives.php .

    To avoid repeating the function twice you should also consider solving this something like so:

     function add_isotope( $isotope ) {
        wp_register_script( 'isotope', get_template_directory_uri().'/js/isotope.pkgd.min.js', array('jquery'),  true );
        wp_register_script( 'isotope-init', get_template_directory_uri().'/js/' . $isotope . '.js', array('jquery', 'isotope'),  true );
        wp_enqueue_script('isotope-init');
     }
    
     add_action( 'wp_enqueue_scripts', 'add_isotope' );
    
     if ( is_page_template( 'archives.php' ) : 
        add_isotope( 'isotope-hideall' );
     else :
        add_isotope( 'isotope' );
     endif;
    
  2. Your complete logic is wrong here. Archives are targeted with is_archive() or the more specific conditional tag is_date() for normal archives. Note that is_archive() returns true on category, author, tag, date and taxonomy pages, so is_archive() might be a bit too generic to use if you only need to target archive

    Also, your conditionals should be inside the function, not outside it as the conditional checks are way too late for the wp_enqueue_scripts hook.

    Your code should be something like this

    function add_isotope() {
        if ( is_archive() ) { // Change as needed as I said
            // Enqueue scripts for archive pages
        } else {
            // Enqueue scripts for all other pages
        }
    }
    
    add_action( 'wp_enqueue_scripts', 'add_isotope' );
    

Comments are closed.