WordPress FAQ 手风琴自定义帖子类型 - 匿名函数问题.



我想在我的wordpress网站中以手风琴风格显示FAQ自定义帖子类型,我在这里找到了一个如何做到这一点的教程:https://code.tutsplus.com/articles/create-an-faq-accordion-for-wordpress-with-jquery-ui--wp-25706

我在函数中添加了所有代码.php:

/* Register the Custom Post Type */
add_action('init', function() {
    $labels = array(
        'name' => _x('FAQ', 'post type general name'),
        'singular_name' => _x('Question', 'post type singular name'),
        'add_new' => _x('Add New Question', 'Question'),
        'add_new_item' => __('Add New Question'),
        'edit_item' => __('Edit Question'),
        'new_item' => __('New Question'),
        'all_items' => __('All FAQ Questions'),
        'view_item' => __('View Question'),
        'search_items' => __('Search FAQ'),
        'not_found' => __('No FAQ found'),
        'not_found_in_trash' => __('No FAQ found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => 'FAQ'
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title', 'editor', 'page-attributes')
    );
    register_post_type('FAQ', $args);
});
add_action( 'wp_enqueue_scripts', 'wptuts_enqueue' );
function wptuts_enqueue() {
    wp_register_style('wptuts-jquery-ui-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/south-street/jquery-ui.css');
    wp_enqueue_style('wptuts-jquery-ui-style');
    wp_register_script('wptuts-custom-js', get_template_directory_uri() . '/faq/faq.js', 'jquery-ui-accordion', '', true);
    wp_enqueue_script('wptuts-custom-js');
}
add_shortcode('faq', function() {
    $posts = get_posts(array(  //Get the FAQ Custom Post Type
        'numberposts' => 10,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post_type' => 'faq',
    ));
    $faq  = '<div id="wptuts-accordion">'; //Open the container
    foreach ( $posts as $post ) { // Generate the markup for each Question
        $faq .= sprintf(('<h3><a href="">%1$s</a></h3><div>%2$s</div>'),
            $post->post_title,
            wpautop($post->post_content)
        );
    }
    $faq .= '</div>'; //Close the container
    return $faq; //Return the HTML.
});

我在FAQ中添加了jquery代码.js如下所示:

(function(){
    jQuery("#wptuts-accordion").accordion();
})();

我收到错误:

Uncaught TypeError: undefined is not a functionfaq.js?ver=4.1:2 (anonymous function)faq.js?ver=4.1:3 (anonymous function)

如何解决这个问题,我做错了什么?我检查了我的主机,我使用的是最新的 PHP 版本,因为我认为这可能是问题所在......

谢谢!

我认为问题出在wp_register_script('wptuts-custom-js', get_template_directory_uri() . '/faq/faq.js', 'jquery-ui-accordion', '', true)

wp_register_script函数的第三个参数(即依赖项)必须是数组。尝试替换它。

wp_register_script('wptuts-custom-js', get_template_directory_uri() . '/faq/faq.js', array('jquery-ui-accordion'), '', true)

而且您还将jquery-ui-accordion设置为此脚本的依赖项,但代码中没有名为jquery-ui-accordion的脚本。您还必须注册该脚本才能获得此工作。

查看法典以获取更多信息。 http://codex.wordpress.org/Function_Reference/wp_register_script

最新更新