Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 );  ?>

Ty

share|improve this question
    
Can you edit your question to include the actual variable definition? –  Chip Bennett Aug 4 '12 at 15:40
add comment

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.

2 Answers

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
add comment

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

if(!$_SESSION['myvar']){

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

}

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

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