WordPress.org

Ready to get started?Download WordPress

Forums

Passing PHP Variable Between Template Files (5 posts)

  1. eddiegiese
    Member
    Posted 4 years ago #

    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?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    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.

  3. lancemonotone
    Member
    Posted 3 years ago #

    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;
  4. este
    Member
    Posted 3 years ago #

    Fantastic lancemonotone !!!!

  5. jcskyrocket
    Member
    Posted 3 years ago #

    Very handy!

Topic Closed

This topic has been closed to new replies.

About this Topic