Skip to main content
Participant
April 30, 2020
Question

Include page

  • April 30, 2020
  • 2 replies
  • 480 views

I need to make changes to my developed site (10 years ago) with the CS6 version to add a single menu for all pages. I created a 'menu' folder containing menu.php.
In the header of my pages, I add the instruction: '<? Php include' menu.php '; ?> '. According to the hierarchy, the pages of the site are not always displayed identically.
Some present the menu, others the menu without the icons included, or no menu. On each page, links to CSS are present.
Can you help me ?
Cordially

    This topic has been closed for replies.

    2 replies

    Nancy OShea
    Community Expert
    Community Expert
    April 30, 2020

    Links inside include files must be relative to the parent document, not the include file.  And if you don't keep all your site pages at the server's root level (same as your index), things tend to break.

     

    Use site root-relative or absolute links in your include files.

    https://helpx.adobe.com/dreamweaver/using/linking-navigation.html

     

    Nancy O'Shea— Product User & Community Expert
    jeanguyTAuthor
    Participant
    May 2, 2020

    I converted all html pages to php.
    Beginner in php, I did not know how to apply your instructions, to place and in which file to apply the changes : menu.php or index.php.
    I used the code : <?php include('menu/menu.php'); ?> for the inclusion of my menu in the header of all pages.
    Depending on the tree structure, the menu may or not appear.
    The attached image indicates to what level the code works.
    I also tested the following code include $_SERVER['DOCUMENT_ROOT']."/menu/menu.php"; which did not have a correct result.
    What can you advise me ?
    Cordialement
    Merci

    Nancy OShea
    Community Expert
    Community Expert
    May 2, 2020

    Your site structure is overly complicated which is a problem. Either simplify your site structure by moving pages to Site Root level or use absolute URLs for navigation links.  Your choice.

     

    SITE ROOT

    animaux.php

    art.php

    galerie.php

    index.php

    etc....

     

    ABSOLUTE URL in menu.php

    <a href="https://yourdomain.com/galerie/animaux/ani.php">Link to Animation</a>

     

     

    Nancy O'Shea— Product User & Community Expert
    Legend
    April 30, 2020

    Always an issue with php includes unless ALL of your pages are in the root of the folder. Whats happening is the links to the assets are incorrect AFTER being included in your pages because some of the pages are in folders another level up from where the links to your assets say they are in the php includes.

     

    I tend to use absolute urls in php include files. So in the php include file I set a $url variable for working locally and one for the remote. I uncomment the correct variable for working in the local or remote environments:

     

    <?php

    // localhost - $url = "http://localhost/nameOfSiteFolder/"

    // remote host - $url = "http://domainName.com/""

    ?>

     

    Then I assign absolute url paths to the links and the image assets in the php include file:

     

    <li><a href="<?php echo $url; ?>pages/about.php">About Us</a></li>

    <img src="<?php echo $url; ?>images/logo.svg" alt="">

     

    You can then store your pages and images in any folder you like in your site folder as the paths to them will always be absolutely assigned instead of relatively assigned.

     

    Only downside is your management skills need to be good as you can sometimes forget to comment out the local $url and uncomment the remote $url when re-uploading the php includes if you make changes to the content in the php include files.