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
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.
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:
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:
Your complete logic is wrong here. Archives are targeted with
is_archive()
or the more specific conditional tagis_date()
for normal archives. Note thatis_archive()
returns true on category, author, tag, date and taxonomy pages, sois_archive()
might be a bit too generic to use if you only need to target archiveAlso, 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