doushi3189 2017-09-06 09:46
浏览 40
已采纳

在自定义帖子类型存档页面中执行脚本时遇到麻烦

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

});
  • 写回答

2条回答 默认 最新

  • dougui4325 2017-09-06 11:38
    关注

    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>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分 合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备