2017-09-06 39 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();

相關問題