WordPress-自定义模板未加载CSS,JS和Head Tag为空

发布于 2025-02-11 10:33:34 字数 2183 浏览 1 评论 0原文

使用template_include挂钩将自定义模板分配给query_var

add_action( 'template_include', function( $template ) {
  if ( (get_query_var( 'checklist_slug' ) == false || get_query_var( 'checklist_slug' ) == '') && (get_query_var( 'checklist_action' ) == false || get_query_var( 'checklist_action' ) == '')) {
      return $template;
  }

  return get_template_directory() . '/includes/checklist_edit.php';
});

function uxwithmarwan_files() {
  wp_enqueue_script('main-university-js', get_theme_file_uri('/build/index.js'), array('jquery'), '1.0', true);
  wp_enqueue_style('custom-google-fonts', '//fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700');
  wp_enqueue_style('university_main_styles', get_theme_file_uri('/build/style-index.css'));
  wp_enqueue_script('font-awesome', 'https://kit.fontawesome.com/5d2df7d4f7.js');
  if ( is_page_template('includes/checklist_create.php') ) {
    wp_enqueue_script('main-university-js', get_theme_file_uri('/build/index.js'), array('jquery'), '1.0', true);
    wp_enqueue_style('custom-google-fonts', '//fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700');
    wp_enqueue_style('university_main_styles', get_theme_file_uri('/build/style-index.css'));
    wp_enqueue_script('font-awesome', 'https://kit.fontawesome.com/5d2df7d4f7.js');
  }
  if ( is_page_template('includes/checklist_edit.php') ) {
    wp_enqueue_script('main-university-js', get_theme_file_uri('/build/index.js'), array('jquery'), '1.0', true);
    wp_enqueue_style('custom-google-fonts', '//fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700');
    wp_enqueue_style('university_main_styles', get_theme_file_uri('/build/style-index.css'));
    wp_enqueue_script('font-awesome', 'https://kit.fontawesome.com/5d2df7d4f7.js');
  } 
}

add_action('wp_enqueue_scripts', 'uxwithmarwan_files');

我有问题如您在这里看到的,模板没有加载任何文件 已加载源的图像 < head>标签是完全空的 空头标签的图像

I have a problem after using template_include hook to assign a custom template to query_var

add_action( 'template_include', function( $template ) {
  if ( (get_query_var( 'checklist_slug' ) == false || get_query_var( 'checklist_slug' ) == '') && (get_query_var( 'checklist_action' ) == false || get_query_var( 'checklist_action' ) == '')) {
      return $template;
  }

  return get_template_directory() . '/includes/checklist_edit.php';
});

even when I use is_page_template to enqueue the scripts and stylesheets it's not working

function uxwithmarwan_files() {
  wp_enqueue_script('main-university-js', get_theme_file_uri('/build/index.js'), array('jquery'), '1.0', true);
  wp_enqueue_style('custom-google-fonts', '//fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700');
  wp_enqueue_style('university_main_styles', get_theme_file_uri('/build/style-index.css'));
  wp_enqueue_script('font-awesome', 'https://kit.fontawesome.com/5d2df7d4f7.js');
  if ( is_page_template('includes/checklist_create.php') ) {
    wp_enqueue_script('main-university-js', get_theme_file_uri('/build/index.js'), array('jquery'), '1.0', true);
    wp_enqueue_style('custom-google-fonts', '//fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700');
    wp_enqueue_style('university_main_styles', get_theme_file_uri('/build/style-index.css'));
    wp_enqueue_script('font-awesome', 'https://kit.fontawesome.com/5d2df7d4f7.js');
  }
  if ( is_page_template('includes/checklist_edit.php') ) {
    wp_enqueue_script('main-university-js', get_theme_file_uri('/build/index.js'), array('jquery'), '1.0', true);
    wp_enqueue_style('custom-google-fonts', '//fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700');
    wp_enqueue_style('university_main_styles', get_theme_file_uri('/build/style-index.css'));
    wp_enqueue_script('font-awesome', 'https://kit.fontawesome.com/5d2df7d4f7.js');
  } 
}

add_action('wp_enqueue_scripts', 'uxwithmarwan_files');

as you see here the template doesn't load any files except the index
image of loaded sources
and <head> tag is totally empty
image of empty head tag

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2025-02-18 10:33:34

使用 wp_enqueue_scripts自动将代码删除到网站的“标题”或“页脚”中,您需要确保wp_head() 在您的模板或模板零件中调用,以及 wp_footer()

wp_footer()函数,如果您使用$ in_footer 代码> 功能调用!

When using the wp_enqueue_scripts hook (or any others that automatically drop code into the "header" or "footer" of the site, you'll need to make sure that the wp_head() is called in your templates or template parts, as well as the the wp_footer().

The wp_footer() function is going to be required namely if you're using the $in_footer boolean argument for your wp_enqueue_script() function calls!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文