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
- Include another HTML file in a HTML file
- Serving precompressed content with Brotli on Apache
- C++ #include guards
- C++ #include guards
- Inserting a PDF file in LaTeX
- How do I run a file on localhost?
- class not declared in scope – even though .h was included
- Already defined in .obj – no double inclusions
- How do you #include files in java?
- How do I include a JavaScript file in another JavaScript file?
- Random not declared in scope
- “Multiple definition”, “first defined here” errors
- Error “Unterminated conditional directive” in cross-referencing headers
- “Fatal error: Cannot redeclare function”
- What is the difference between a .cpp file and a .h file?
- Difference between require, include, require_once and include_once?
- How to include file in a bash shell script
- cmath vs math.h (And similar c-prefixed vs .h extension headers)
- How to add a default include path for GCC in Linux?
- Compiler error C4430: missing type specifier – int assumed [duplicate]
- Eclipse CDT: Symbol ‘cout’ could not be resolved
- how to get wp-content folder in wordpress?
- Passing a variable to get_template_part
- get_template_directory_uri pointing to parent theme not child theme
- How to include a file using get_template_part() in a plugin?
- get_template_part vs action hooks in themes
- The proper way to include/require PHP files in WordPress
- Using a custom WP_Query with get_template_part loop
- Is it possible to override the result of get_template_part()?
- How do I replace a function, declared inside a plugin’s class, in functions.php?
- is_plugin_active() returning false on active plugin
- Unable to check if plugin is active
- How to include a plugin’s php file to another plugin functions file [duplicate]
- Include WP_Query in my own PHP file?
- Order by & include array by specific post ids
- Include a external PHP file into a WordPress Custom Template
- Include PHP file in Content using [shortcode]
- When is get_template_part() preferable to simply using the template.php files?
- How to add custom content template part for a custom post type on main query using a plugin
- get_template_part from plugin
- Combining shortcode and get_template_part
- Include files in functions.php
- What is the right way to include a wp-admin file in your theme?
- How to prevent wordpress from loading old versions of jquery in wp_head();?
- Variable use in get_template_part
- Is it safe/recommended to use wp_enqueue_script function outside the functions.php file?
- Problem requiring/including plugin files with plugin_dir_path( __FILE__ )
- Get the php template file from other theme folder
- load/require specific php files for specific pages/templates/post types
- Loading page content into a variable in template
- How can I include JavaScript that use jQuery on admin side
- Why does including a file in theme’s functions.php not work?
- bloginfo(‘stylesheet_directory’) vs. get_stylesheet_directory_uri() and include(‘file.php’) vs. get_template_part()
- How can I see what template parts are being called for rendering the viewable page?
- Get template part vs locate template function
- Sort users in get_users() in custom order
- ob_get_clean returns empty string, ob_get_flush outputs string
- How can I avoid code duplication for a blog with lots of source code?
- get_template_part execute with ajax