WordPress.org

Support

Support » Themes and Templates » Passing PHP Variable Between Template Files

Passing PHP Variable Between Template Files

  • eddiegiese

    @eddiegiese

    I’m trying to pass a PHP variable ($pageTitle) that I define in the top section of each template page (i.e. 404.php) so that I can call up a specific CSS file for unique page styling. It works when I hard-code the variable into the header.php file, which is checking for the $pageTitle variable, but not when I put it in an individual page and try to pass it to the header.php. I’ve done this on multiple other sites that don’t use WordPress without a problem. Can WordPress not support this basic functionality? Is there an additional way to define this in WordPress?

Viewing 4 replies - 1 through 4 (of 4 total)
  • esmi

    @esmi

    Forum Moderator

    WordPress won’t let you include variables in the same way that you might do on a non-WP site. One way to approach this might to use a custom field on your pages and use the value in that field to determine which stylesheet to load.

    http://codex.wordpress.org/Using_Custom_Fields

    Or perhaps you could use body_class() if you simply want to style certain blocks differently.

    I don’t know if you can pass variables between theme files but you can define a variable in your theme’s functions.php file, then at the top of each template file declare it in the global space:

    functions.php

    $some_var = 'Hello World!';

    footer.php (for example)

    global $some_var;
    
    echo $some_var;

    Fantastic lancemonotone !!!!

    Very handy!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Passing PHP Variable Between Template Files’ is closed to new replies.
Skip to toolbar