How to pass variables to get_template_part() in WordPress

Ok! So you need to pass variables to get_template_part() in WordPress. Sorry because get_template_part() is not made to accept variables or arguments. But here is a better way to achieve the same using WordPress’s locate_template() function within PHP’s include().

Passing variables in WordPress to PHP script is done like this:

Notice that we have used template file name along with its extension. In this way all of the variables available in your current script will be available in that template file now too.

So the title of article is quite misleading but the concept is straightforward to pass variables to template files created in WordPress. 🙂

You Might Interested In

4 COMMENTS

  1. Kerstin Cordova says:

    Awesome blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements would really make my blog shine.
    Please let me know where you got your design. Thanks a lot

    Reply
  2. Houston Mccambridge says:

    I do not even know how I ended up here, but I thought this post was great. I do not know who you are but certainly you’re going to a famous blogger if you are not already Cheers!

    Reply
  3. Samual Musgraves says:

    I came to this page by searching google. I have located it quite interesting. thank you for providing this. I will have to visit here again!

    Reply

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.