WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have declared a variable in my single.php

$title  = 'myvar';

Can I get it in my comments.php without re-declaring it again?

I use this code to get the comments.php templage:

<? comments_template( '', true );  ?>


share|improve this question

closed as off topic by Michael, Chip Bennett, Chris_O, Brian Fegter, kaiser Aug 28 '12 at 20:14

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Can you edit your question to include the actual variable definition? – Chip Bennett Aug 4 '12 at 15:40
up vote 3 down vote accepted

The comments_template() only accepts two parameters: $file (string) and $separate_comments (Boolean). So, it does not have a way to pass an arbitrary variable as a parameter.

The two methods I generally use are:

  1. Globalize the variable
  2. Wrap the variable inside a function that returns the data you want, and then call it wherever you need it.
share|improve this answer

You can use php session variables to utilize the myvar variable anywhere you want.


    $_SESSION['myvar'] = 'myvar';


echo $_SESSION['myvar'];
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.