Support

Account

Home Forums General Issues Using a variable on multiple templates

Solving

Using a variable on multiple templates

  • Hello 🙂 please shine some light on this for me, please.

    If using an image field set to array.
    on my template I set a variable:

    $myImage = get_field(‘my_image’);

    then to use it

    ” alt=””>

    this is working fine but If I want to use the same variable on another template, what is the way to go?
    Should I set my variable not on the template but somewhere else?

  • Your variable is only valid in the template where it is set. You need to define it in every template

    
    $myImage = get_field('my_image');
    
  • @hube2 Thank you for your reply.
    Is there some places in WordPress where I can store my variables to use them on any template?
    it seems redundant to reset the variables on every template …

  • You can use a global variable, but his is considered a bad practice and makes code difficult to read, and it will also only work if the file where you declare the variable is always loaded. This may not be the case when dealing with WP template files where only the needed files are loaded.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Using a variable on multiple templates’ is closed to new replies.