0

I have a page with several tabs and to keep things organized I have one file for the overall page template and one for each tab that will be opened. When pressing the tabs I hide/unhide (with the class hidden which sets display: none) sections which is originally placed like this in my page_template.php file:

<div id="content-wrapper">
    <section id="page_tab_1">
        <?php get_template_part( 'page/page_tab_1' ); ?>
    </section>
    <section id="page_tab_2" class="hidden">
        <?php get_template_part( 'page/page_tab_2' ); ?>
    </section>
</div>

Now I want to use a variable in both page_template.php and page_tab_1.php. When placing $my_variable = 'Test'; at the top of the page_template.php I can of course use it below with e.g. <?php echo $my_variable; ?>, but when I try to echo it in page_tab_1.php it doesn't work.

I solved it by adding functions at the top of page_template.php like this:

function get_my_variable(){return 'Test';}

Then I can use the following code in both files:

$my_variable = get_my_variable();

The reason I don't want to put $my_variable = 'Test'; in both files is because some of the real variables take some calculations and I don't want to duplicate too much code.

Question

I thought I could just declare the variables at the top of page_template.php since the other code is pasted below that, but I guess that is not how get_template_part works. So is there a more elegant/smart way to declare variables in page_template.php which can then be used by all page_tab_x.php files?

6
  • Did you consider using the SESSION? Commented Jan 3, 2023 at 19:05
  • @RiggsFolly, I don't know what that is. Commented Jan 3, 2023 at 19:10
  • Then you probably should not be playing with WordPress coding yet Commented Jan 3, 2023 at 19:11
  • @RiggsFolly, I have a working website with 1000s of customers with a WP theme I built much on my own. Comments like that won't help me with my issue, so if you know an answer to my question then please explain it and then use the "Post Your Answer" button below. Commented Jan 3, 2023 at 19:13
  • php.net/manual/en/reserved.variables.session.php or geeksforgeeks.org/php-sessions Commented Jan 3, 2023 at 19:15

1 Answer 1

3

get_template_part let you pass additional arguments to the template through the third parameter $args.

@see https://developer.wordpress.org/reference/functions/get_template_part/

get_template_part(
    'template-part',
    'name',
    array(
        'key1'  => $myValue1,
        'key2'  => 'myValue2',
    )
);
Sign up to request clarification or add additional context in comments.

6 Comments

Oh, forgot about this one. This is the way.
The link amarinediary shared explains that part as well, @eligolf
@cabrerahector, I tried what is said in the document by adding the argument "array('key1' => "test")" and then tried to use $args['key1'] but it outputs nothing. Am I missing something?
That's odd. Which WordPress version are you using over there? Edit: Wait, the third argument needs to be an array (as seen on the example above). Are you passing a string to it?
@cabrerahector, I am stupid, the previous developer had a middle function which called get_template_part with only 2 arguments. When I call it myself directly with 3 arguments it works perfectly. Sorry for being stupid and thank you for the help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.