chasethesunnn
chasethesunnn

Reputation: 2224

Cant echo a variable that is on the same page but different file

I have included header.php and index.php on the same page.

The following can be found on the header.php file:

<?php 
    $pageName="home";
    if(is_page('47')){
       $pageName="services";}
    printVar($pageName);
?>

function printVar($v){ //on functions.php
    echo $v;
}

It prints out the var immediately after the if-statement and anywhere on header.php, but when I go to index.php it doesn't print.

<ul class="<?php printVar($pageName); //does not print out ?>">  
    <li class="home"><a href="/">Home</a></li>
    <li class="services"><a href="/">Services</a></li>
    <li class="seminar"><a href="#">Seminar</a></li>
    <li class="contact last"><a href="#">Contact</a></li>
</ul>

How can I print this variable without moving everything to index.php?

Upvotes: 0

Views: 121

Answers (2)

Devin Crossman
Devin Crossman

Reputation: 7592

you need to have

<?php get_header(); ?>

in your index.php file

read more about get_header() on wordpress function reference.

Upvotes: 2

Whippet
Whippet

Reputation: 322

Try adding:

include("header.php");
include("functions.php");

to your index.php

Add:

include("header.php");

in functions.php

and add:

include("functions.php");

in header.php

Upvotes: 1

Related Questions