【问题标题】:Linking CSS file in php document nested in pages folder在嵌套在页面文件夹中的 php 文档中链接 CSS 文件
【发布时间】:2020-11-03 17:58:12
【问题描述】:

Web 开发新手在这里。我的网站变得足够大,最近采取了一些措施来帮助管理它,包括:

  1. 使用单独的 header.php;页脚.php;和带有包含标签的navigation.php 文件。
  2. 在 root/pages 下归档了其他页面。以下是我当前文件夹的快照:
    • 资产
    • 图片
    • js
    • php

我现在的问题是我在 pages 文件夹中嵌套了其他页面(除了 index.html)。在对这些文件进行编码时,链接/包含文件会导致问题,因为相对/绝对文件引用。通过一些研究,我能够使用这个似乎适用于页眉、页脚和导航菜单的 php 代码:

<?php 
set_include_path( implode( PATH_SEPARATOR, array(
$_SERVER['DOCUMENT_ROOT'],
get_include_path()
) ) );

include 'root/assets/header.php';

?>

我现在遇到的问题是 css 链接也不再有效:

 <link rel="stylesheet" href="css/style-services.css">

我试过这样做,但没有运气:

<?php 
set_include_path( implode( PATH_SEPARATOR, array(
$_SERVER['DOCUMENT_ROOT'],
get_include_path()
) ) );

include 'root/css/style-services.css';

?>

如何确保在使用文件结构和 php 包含函数时也包含引用的 css?

【问题讨论】:

    标签: php html css file-structure


    【解决方案1】:

    不要包含 CSS 文件,只需 echo 即可。喜欢:

    <?php
    //
    // your other cods here
    //
    $style_services = file_get_contents ("root/css/style-services.css");
    echo $style_services;
    //
    // your other cods here
    //
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-01-16
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 2021-01-30
      • 2014-04-11
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多