I have a custom post type archive page where I'd like to execute a specific script for the page. The script is loaded in the page with this function (in function.php):
function cpt_archive_enqueue_script() {
if (is_post_type_archive('cpt-slug')) {
wp_enqueue_script( 'cpt-archive-script', get_stylesheet_directory_uri() . '/js/cpt-archive-script.js', array( 'jquery' ), '1.0.0', true );
};
}
add_action ('wp_enqueue_scripts', 'cpt_archive_enqueue_script');
As I can see with page inspector, the page loads the script but the loaded script is not executed ( the same script work f.e. if loaded in a category page).
Have anyone a suggestion to fix this? Thanks!
If can help here is the loaded script (it is a really simple script that opens the article content if the title is clicked).
jQuery(document).ready(function($) {
$('article.post').each(function() {
var $dropdown = $(this);
$("div.entry-title", $dropdown).click(function(e) {
e.preventDefault();
$div = $("div.entry-content", $dropdown);
$div.toggle();
$("div.entry-content").not($div).hide();
return false;
});
});
$('html').click(function(){
$("div.entry-content").hide();
});
});
Try with below code it must be work on each page.
add_action('wp_head','callfunctioneverywhere');
function callfunctioneverywhere()
{
echo '<script defer src="'.get_stylesheet_directory_uri() .'/js/cpt-archive-script.js" ></script>';
}
Here is the whole code:
jQuery(document).ready(function($) {
$('article.post').each(function() {
var $dropdown = $(this);
$("div.entry-title", $dropdown).click(function(e) {
e.preventDefault();
$div = $("div.entry-content", $dropdown);
$div.toggle("blind", 300);
$("div.entry-content").not($div).hide("blind", { direction: "up" }, "300");
return false;
});
});
$('html').click(function(){
$("div.entry-content").hide("blind", { direction: "up" }, "300");
});
});
.hide {display: none;}
<?php
//* add custom classes
add_filter( 'body_class', 'journal' );
function journal ( $classes ) {
$classes[] = 'journal';
return $classes;
}
//* Remove the link from each post title
add_filter( 'genesis_post_title_output', 'elimina_link_titolo', 15 );
function elimina_link_titolo( $title ) {
$title = sprintf( '<div class="entry-title five-sixth mostra">%s</div> ', apply_filters( 'genesis_post_title_text', get_the_title() ) );
return $title;
}
//* Add the 'hide' class ( .hide {display: none;} )to not show the content that will appear by clicking on title
add_filter ('genesis_attr_entry-content', '\margine_sx_un_terzo');
function margine_sx_un_terzo ( array $attributes ) {
$attributes['class'] .= ' hide';
return $attributes;
}
genesis();
</div>