Tell me more ×
WordPress Answers 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

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

Questions on WordPress Answers are expected to relate to WordPress within the scope defined in the FAQ. 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 closed questions here.

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

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

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