2017-09-06 113 views
0

我有一个自定义帖子类型存档页面,我想为该页面执行特定的脚本。该脚本加载该页面使用此功能(在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'); 

,我可以用页面检查员看到,页面加载脚本,但不执行加载脚本(相同的脚本工作FE如果加载在类别页面中)。

有没有人有解决这个问题的建议?谢谢!

如果可以帮助这里是加载的脚本(这是一个非常简单的脚本,如果标题被点击打开文章内容)。

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(); 
}); 

}); 
+0

您使用的儿童主题? –

+0

是的,该网站使用发源框架和样本子主题 – simba

回答

0

请尝试下面的代码,它必须在每个页面上工作。

add_action('wp_head','callfunctioneverywhere'); 
function callfunctioneverywhere() 
{ 
    echo '<script defer src="'.get_stylesheet_directory_uri() .'/js/cpt-archive-script.js" ></script>'; 
} 
+0

谢谢,但它仍然无法正常工作,您更改了脚本的名称并删除了版本,对不对?或者我想念什么? – simba

+0

如果条件不起作用? –

+0

不幸的是不适用于自定义帖子类型的档案,没有条件只适用于类别档案或帖子... – simba

0

这里是整个代码:

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) { 
 

 
\t $classes[] = 'journal'; 
 
\t return $classes; 
 

 
} 
 

 
//* Remove the link from each post title 
 
\t add_filter('genesis_post_title_output', 'elimina_link_titolo', 15); 
 
\t \t function elimina_link_titolo($title) { 
 
\t   $title = sprintf('<div class="entry-title five-sixth mostra">%s</div> ', apply_filters('genesis_post_title_text', get_the_title())); 
 
\t \t \t return $title; 
 
\t \t } 
 

 
//* 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'); 
 
\t function margine_sx_un_terzo (array $attributes) { 
 
\t \t $attributes['class'] .= ' hide'; 
 

 
\t \t return $attributes; 
 
\t } 
 

 
genesis();

相关问题