SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot allanlud's Avatar
    Join Date
    Jul 2012
    Location
    Meath, Ireland
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding a line of PHP to each page making them unique

    Hi,

    I'm not great with Wordpress, new enough to it (and PHP).

    Sites I have worked on before would have page files like, index.php or about-us/index.php - so in each of these files at the top would be something like this:

    PHP Code:
    $currentLevel1Page "Home"
    Anyone know how I can add something like this to separate pages in Wordpress? I'd like to call a different element, like a paragraph or menu depending on the page.

    Cheers.

    Al.

  2. #2
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    429
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't know if this is the best way, but I add
    Code:
    if (is_page()) { 
    	$page_slug = 'page-'.$post->post_name; 
    }
    at the top of the page (in header.php) and then add the class to the body tag with
    Code:
    <body <?php body_class($page_slug); ?>>
    That way if I have a page that has the title "About Us", the body tag will have a class of "page-about-us" among other default classes that WordPress uses. So if I need to target just that page, I can refer to its class name.

  3. #3
    SitePoint Zealot allanlud's Avatar
    Join Date
    Jul 2012
    Location
    Meath, Ireland
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that WebMachine.

    I got a comment in another forum which said to use $_SERVER['request_uri'].

    What I have at the moment is:

    Code:
    <?php
    	if($_SERVER['REQUEST_URI'] == '/')
    		{
    		echo 'content';
    		}
    ?>
    And that seems to work. What my problem now is, that I need to get an elseif statement working to get the above code to output different content for whatever page it is on.

    I have tried this, but I'm not even sure if it's right (it doesn't seem to work though):

    Code:
    <?php
    	if($_SERVER['REQUEST_URI'] == '/')
    		{
    		echo 'content';
    		}
            elseif($_SERVER['REQUEST_URI'] == '/about/')
    		{
    		echo 'about content';
    		}
    ?>

  4. #4
    SitePoint Zealot allanlud's Avatar
    Join Date
    Jul 2012
    Location
    Meath, Ireland
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have it sorted now using this:

    Code:
    $page = $_SERVER['REQUEST_URI'];
    if ($page == '/') {
    
       $content = 'page /';
    
    } else if ($page == '/home') {
    
       $content = 'page /home';
    
    } else if ($page == '/about') {
    
       $content = 'page /about';
    
    } else if ($page == '/contact') {
    
       $content = 'page /contact';
    
    } else {
    
       $content = 'default if page is not above';
    
    }


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •