0

「header.php」ファイルがあり、次のコードを使用して HTML ページに組み込んでいます。

<?php /*
            $pageTitle = "Home";
            $section = "Home";
            include('inc/header.php');
            ?>

PHP ヘッダー ファイルには、多数の CSS スタイルシートが含まれています。これらの CSS スタイルシートのいくつかは、1 つのページにのみ含めたいと考えています。たとえば、「index.css」をページ「index.php」にのみ含めたいと考えています。サイトのすべてのページで同じ「header.php」ファイルを使用している場合、これらのスタイルシートを含めるにはどうすればよいですか?

ヘッダー ファイルではなく、個々のページ自体でこれらのスタイルを指定する方法はありますか? その場合、要素がヘッダー ファイルで既に使用されている (開いて閉じている) 場合でも、これらのスタイルを別の要素に配置できますか?

「header.php」ファイルのコードは次のとおりです。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html;">
    <title><?php echo $pageTitle ?> || Young Academy </title>

    <meta name="description" content="Based in Pinner in North-West London, the Young Academy has been creating the highest standard of musical and dramatic education for 30 years.">
    <meta name="keywords" content="Piano, Flute, Music Theory, Singing, Violin, Concerts, Pinner, Middlesex, Harrow, Teacher">
    <meta name="author" content="Barbara Young">


    <!--Stylesheets!-->

    <link rel="stylesheet" type="text/css" media="all" href="css/reset.css"/>
    <link rel="stylesheet" type="text/css" media="all" href="css/text.css" />
    <link rel="stylesheet" type="text/css" media="all" href="css/960_16_col.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="css/style.css"/>
    <link rel="stylesheet" type="text/css" media="screen" href="css/contact.css"/>


    <!--Google fonts!-->

    <link rel="stylesheet" type="text/css" media="all" href="http://fonts.googleapis.com/css?family=Quicksand:400,700">
    <link href='http://fonts.googleapis.com/css?family=Titillium+Web' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Noto+Serif:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Karla:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Marck+Script' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,700,500,500italic,400italic' rel='stylesheet' type='text/css'>
    **<link rel="stylesheet" type="text/css" media="screen" href="css/index.css"/>
    <link rel="stylesheet" type="text/css" media="screen" href="css/bgaudioplayer.css"/>**
    <!--Meta tags!-->

    <meta name="author" content="Robert Young" />
    <meta name="copyright" content="2013 by Robert Young" />
    <meta name="keywords" content="Music lessons, Pinner, music tuition, piano lessons, speech and drama, London" />
    <meta name="description" content="Site of the Young Academy, a private music tuition business based in Pinner" />


<script src="../js/modernizr.custom.63826.js"></script>
<script src="../js/html5shiv.js"></script>


</head>

<body>

    <div class="header">

        <div class="container_16 clearfix">
            <ul class="nav grid_16 alpha">
                <li><a class="<?php if ($section == "Home") {echo "here"; } ?>" href="index.php" accesskey="1"> Home </a></li>
                <li><a class="<?php if ($section == "About Us") {echo "here"; } ?>" href="about_us.php" accesskey="2"> About </a></li>
                <li><a class="<?php if ($section == "What We Do") {echo "here"; } ?>" href="what_we_do.php" accesskey="3"> What We Do </a></li>
                <li><a class="<?php if ($section == "Enrolement") {echo "here" ; } ?>" href="enrolement.php" accesskey="4"> Enrolement </a></li>
                <li><a class="<?php if ($section == "Contact Us") {echo "here" ; } ?>" href="contact.php" accesskey="5"> Contact Us </a></li>
                <!--<li><a href="http://youngacademyblog.blogspot.co.uk/" accesskey="6"> Blog </a></li>-->
            </ul>


            <div id="logo">
                <a href="index.php"><img src="img/Logo original.gif" alt="The Young Academy"/></a>
            </div>
            <div class = "grid_4 alpha">

            <div class="telephone grid_4 alpha">
                <img src="img/phone_icon_white.png" alt="small telephone logo" class="align-left small" />
                <p> +44 (0)20 8866 3813 </P>
            </div>
            <div class="timezone">

                    <p><?php
                    date_default_timezone_set('Europe/London');
                    mktime(0,0,0,1,1,1970);
                    echo date('l, d F Y'); 
                    ?> </p>

                    <!--<form action="http://www.example.com/login.php">
                        <p>

                            <input type="text name=search" size="20+" id="search" value="Search this site"
                        </p>
                    </form>!-->



            </div>

        </div>


                <ul class="secondmenu grid_6 push_3">

                    <li><a class="<?php if ($section == "Musical Glossary") {echo "here" ; } ?>" href="glossary.php">Musical Glossary </a></li>
                    <li><a class="<?php if ($section == "FAQ") {echo "here" ; } ?>" href="faq.php">FAQ</a></li>

                <ul>






    </div>

index.php ページのみに太字のスタイルシートを含めたい。次のように含めることはできますか:

<?php 

            include('inc/header.php');
            ?>


<head>
            <link rel="stylesheet" type="text/css" media="screen" href="css/index.css"/>
            <link rel="stylesheet" type="text/css" media="screen" href="css/bgaudioplayer.css"/>

</head>






                    <article class="content">

                        <p class="grid_9 alpha"><i>Outstanding vocal and instrumental tuition.</i></p>

                    </article>

                    <article class="second-content">

                    <p class="grid_6 alpha omega">Develop a passion for music.</p>

                    </article>

                    <article class="third-content">

                    <p class="grid_5 alpha omega">Become a virtuoso.</p>

                    </article>

どんな助けでも大歓迎です!

ありがとう、

ロバート。

4

3 に答える 3

0

これは、CSS へのリンクを置き換える if else ステートメントを使用して行うことができ、CSS を取得するページに送信した一意の変数を参照します。

  <link rel="stylesheet" type="text/css" media="screen" href="

<?php
if ($pageTitle == "Home") {

echo "css/index.css";


else ($pageTitle == "SomeOtherTitle") {

echo "css/relativelnk.css";
}

?>
   "/>
于 2013-07-18T22:13:20.657 に答える
0

上記のコードを試してみたところ、たまたまうまくいきました!head タグは、header.php ファイルとメイン (index.php) サイト ファイルの両方に含めることができます。

乾杯!

于 2013-07-18T22:08:28.760 に答える
0

要するに、ある種のルーターが必要です。しかし、あなたのコードを見ると、オブジェクト指向プログラミングについてしっかりと把握していないように見えます。ただし、できることは、$_SERVER['REQUEST_URI']で URI を調べることです。文字列が /about である場合は、about-stylesheet.css のリソースをロードします。

于 2013-07-18T22:11:53.367 に答える