• Hey all, I looked around briefly but couldn’t find a lot of information that would help me out. I’m building a custom content-single.php, and I’d like for it to be a template that uses php variables that will eventually be called from a SQL database. For now though, I’d like to assign the variables in the_content() if possible.

    I have the ‘PHP Exec’ plugin installed, which allows php to be inserted in posts between <phpcode> tags, and it seems to be working fine.

    As a test, I initialized a test variable on the actual content-single.php above the call for the_content() with

    <php
         $address = 'default';
    ?>

    then in the actual wordpress content editor, i’ve written

    <phpcode>
         <?php
              $address = 'success';
         ?>
    </phpcode>

    back to content-single.php, after the template calls the_content(), I’ve got

    <?php
         echo $address;
    ?>

    which is returning “default” on the actual page.

    So with a little bit of research, it looks like a problem of the variable’s scope because I’m assigning it within a function. Do I use globals here? I’m not sure exactly how to proceed, any advice would be appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • This is not an answer to th77409, but addresses the same issue: the scope of “Global”. Maybe someone can answer both with one post.

    Ok, I’m wading into WordPress PHP. My coding experience is only in Visual Basic. There, the Local and Global variables are declared with the implication that the Local declaration will be made available only to that subroutine and the Global to all routines in the project.

    I mention this because I can’t quite get a handle on “Global” as it’s used in WordPress. Do we go into a template and declare a variable as “global” and thereby make it’s value available to all templates? And, conversely, if we create an undeclared variable, is its value invisible to other templates?

    THanks in advance

    in PHP, variables that are defined inside of a function are only able to be used in that function. You need to declare a variable as GLOBAL if you want to be able to use it inside and outside of the function

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Assigning Template Variable from a Post?’ is closed to new replies.