WordPress自定义类别页面未加载javasctipt文件并给出404错误

Wordpress custom category page not loading javasctipt files and giving 404 error

本文关键字:文件 错误 javasctipt 加载 自定义 WordPress      更新时间:2023-09-26

我从头开始开发一个主题,现在我有一个来自一个网站的代码片段,其中他正在使用这样的脚本 -

<script>
        head.js(
            { jquery : "js/jquery.min.js" },
            { mousewheel : "js/jquery.mousewheel.js" },
            { mwheelIntent : "js/mwheelIntent.js" },
            { jScrollPane : "js/jquery.jscrollpane.min.js" },
            { history : "js/jquery.history.js" },
            { stringLib : "js/core.string.js" },
            { easing : "js/jquery.easing.1.3.js" },
            { smartresize : "js/jquery.smartresize.js" },
            { page : "js/jquery.page.js" }
        );
</script>

现在,当我在我的类别中尝试此操作时-{slug}.php它会在文件夹中搜索这些文件 wordpress/category/{slug}/

现在我也在这里尝试了"

<script>
        head.js(
            { jquery : "<?php bloginfo('template_url')?>/js/jquery.min.js" },
            { mousewheel : "<?php bloginfo('template_url')?>/js/jquery.mousewheel.js" },
            { mwheelIntent : "<?php bloginfo('template_url')?>/js/mwheelIntent.js" },
            { jScrollPane : "<?php bloginfo('template_url')?>/js/jquery.jscrollpane.min.js" },
            { history : "<?php bloginfo('template_url')?>/js/jquery.history.js" },
            { stringLib : "<?php bloginfo('template_url')?>/js/core.string.js" },
            { easing : "js/jquery.easing.1.3.js" },
            { smartresize : "<?php bloginfo('template_url')?>/js/jquery.smartresize.js" },
            { page : "<?php bloginfo('template_url')?>/js/jquery.page.js" }
        );`
</script>

然后我也在火虫中收到 404 错误,但是当我尝试这样的事情时 -

{jquery : "../../wp-content/themes/testing/js/literature"},

它有效,现在我想知道为什么它在文件夹类别而不是我的主题目录中查找这些依赖项,而同时如果我写了这些行

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/literature/head.min.js"></script>

在我的脑海部分,令我惊讶的是它们工作,谁能告诉我这里发生了什么

根据你的代码,你正在使用一个名为head.js的脚本,它允许你向标头添加更多脚本。如果你真的想使用 head.js,你必须手动在脑海中添加该脚本。我不鼓励使用你并不真正需要的东西。

把这个PHP代码放在你的函数.php文件中,该文件应该位于你的主题目录中。如果没有,请创建它。这就是您通常将脚本包含在主题中的方式。

add_action('wp_enqueue_scripts', 'mytheme_enqueue_scripts');
function mytheme_enqueue_scripts() {
    // jQuery comes with WordPress, no need to include it

    $dir = get_stylesheet_directory_uri() . '/js';
    // Vanilla scripts
    wp_enqueue_scripts('stringLib',     $dir . '/core.string.js');
    // All these guys depend on jQuery, hence the "array('jquery')"
    wp_enqueue_scripts('easing',        $dir . '/jquery.easing.1.3.js',         array('jquery'));
    wp_enqueue_scripts('easing',        $dir . '/jquery.smartresize.js',        array('jquery'));
    wp_enqueue_scripts('easing',        $dir . '/jquery.page.js',               array('jquery'));
    wp_enqueue_scripts('history',       $dir . '/jquery.history.js',            array('jquery'));
    wp_enqueue_scripts('mousewheel',    $dir . '/jquery.mousewheel.js',         array('jquery'));
    wp_enqueue_scripts('mwheelintent',  $dir . '/jquery.easing.1.3.js',         array('jquery'));
    wp_enqueue_scripts('mwheelintent',  $dir . '/mwheelIntent.js',              array('jquery'));
    wp_enqueue_scripts('jscrollpane',   $dir . '/jquery.jscrollpane.min.js',    array('jquery'));
    // This one happens to rely on jScrollPane
    wp_enqueue_scripts('mwheelintent',  $dir . '/mwheelIntent.js',              array('jscrollpane'));
}

不要忘记PHP代码总是以<?php开头

wp-blog-header.php 在加载 category-{slug}.php 之前是否加载