Like MathSmath wrote, get_template() does not support the re-use of your variables.
But locate_template() infact does no inclusion at all. It just locates a file for inclusion.
So you can make use of include to have this working just like you expect it:
include(locate_template('custom-template-part.php'));
$var
from your example can be used in the template part then.
A related question with a more technical explanation of the variable scope and get_template(): Form Submitting Error with get_template_part()
Related Posts:
- Is there any way to use get_template_part() with folders?
- How to use get_template_part()?
- Should we use get_template_part() in functions files instead of include_once?
- Is there a variable for a template parts name?
- How to make get_template_part always check child theme first?
- Check if partial file is called from within header.php or footer.php
- Too many get_template_parts?
- get_template_part() does not work if you call it when you are in a subfolder
- Pass a variable to get_template_part
- passing argument to get_template_part() or a better way to code
- get_template_part inside get_template_part?
- Getting the chosen template when iterating through pages
- Passing variables to template parts
- Possible to use pagename inside get_template_part?
- Why would include(locate_template) work but not get_template_part()?
- How does get_template_part() work inside a loop?
- Is there a tirck in the get_xxxx function in the general_template.php file?
- How to pass variables with get_template_part?
- Problem with using get_template_part() in footer
- how to remove get_template_part from a plugin? [closed]
- get_template_part() based on get_post_type() for a custom post type instead of get_post_format()
- Using get_template_part to retrieve a template file based on current post type
- add a hook with get_template_part()
- ä/ü/… are not displayed when including via get_template_part()
- Change the file path on get_template_part via plugin
- How to limit the number of Related Posts?
- get_template_part – how to find html file which is being render
- Get_Template_Part And Two Global WP_Query On Frontpage
- The use of get_template_part() returns ‘invalid argument’ loading options
- Passing a variable from the archive page to the default template
- For which reasons can get_template_part’s $args fail to work? (WP 5.8.1 – PHP 7.4.23)
- Can I set a global base path for get_template_part?
- Adding a second loop breaks everything
- how to use a different template (index.php) in wordpress
- get_template_part for template in subdirectory not working
- What are the benefits of using get_template_part()?
- Include template file from plugin to theme
- get_template_part doesn’t output all data
- Using require_once for parent-directory not working but is working in sub-directories
- Theme parts path location dump
- strange parse error when including a loop template within another template
- C++ #include guards
- How do I include a JavaScript file in another JavaScript file?
- Combining shortcode and get_template_part
- Variable use in get_template_part
- Problem requiring/including plugin files with plugin_dir_path( __FILE__ )
- Get template part vs locate template function
- How can I avoid code duplication for a blog with lots of source code?
- Show certain terms from custom taxonomy but exclude ‘parent’ terms?
- How to include template according to meta post value
- Accessing $post variable from template part
- Using Includes in Templates in Document Head
- where to include a php file
- get_template_part not working with ajax
- Why does get_template_part() break variables?
- How to localize inline script called with ajax
- Conditional Ajax inclusion
- Can I change a variable in a content part while calling it?
- the_author() not working outside the loop
- How to include core files into plugin
- How to query posts to include specific pages of one post type, and all pages of another post type?
- Include content from one page to another
- Where is the template for the ‘standard’ format of a post in the theme twentythirteen?
- get_template_part and template file names
- how could I load a different template part by page
- Problems with loop
- Conditional get template part
- Calling a Function in Functions.php from an external PHP script
- code is skipping a div
- Detect if current page is front page within custom query
- Correct and safe way to include php content in my page
- php include not working in custom page
- Custom post type index (maybe using get_template_part)
- How to pass a variable to get_template_part that’s updated every time the template part is called?
- How can I include an external file in a post or page?
- Does get_template_part() content gets parsed if it’s not executed?
- Get_template_part seems to be ignoring my template
- Is there a way to retrieve the calling template part?
- Is it possible to have a Theme with built-in physical page files?
- can’t access some WordPress function from my plugin
- On click some element i want to use some template
- Pull in an “Include” file based on a WordPress Category ID
- Template not loading with get_template_part or locate_template
- Load templates, pass arguments, and render output from functions.php
- I’m including a html file in my plugin, but it can’t find its css or js files
- Ajax and get_template_part
- Is_Page doesnt detect my page
- Display a custom 404 page without a redirect
- Remove a short code and update it’s content (AJAX)
- How to get value from radio buttons meta box, and return different contents?
- When to use content-pagename.php?
- post content includes and seo vs. duplicate content
- how to modify request to get_template_part()?
- If I define a variable in header.php, how do I make it available to templates?
- White Screen When Includding A Folder With PHP Files
- wordpress.org disallowing my plugin becuase of loading core files in cron cpanel file
- Javascript file not included only on home page
- Headers already sent error with get_template_part in REST API call
- get_template_part() does not render after the ajax request
- WP-Include rewrite directory