php - How to create dynamic templates?

admin2025-06-03  3

So let's say that I have a navbar template with the following content:

// template_parts / navbar.php

<nav class="navbar navbar-expand-lg dark bg-transparent fixed-top" id="mainNav">
  <div class="container">
    <a class="navbar-brand" href="./">
     This is the company name
    </a>
  </div>
</nav>

Is there a way to pass a variable to the template in order to change, in this case, the company's name when calling the template?

Something like get_template_part("template_parts/navbar","Microsoft");

I know the above syntax works for another purpose, I'm just trying to illustrate what I want to achieve.

Is there a way to do this? Thanks beforehand!

So let's say that I have a navbar template with the following content:

// template_parts / navbar.php

<nav class="navbar navbar-expand-lg dark bg-transparent fixed-top" id="mainNav">
  <div class="container">
    <a class="navbar-brand" href="./">
     This is the company name
    </a>
  </div>
</nav>

Is there a way to pass a variable to the template in order to change, in this case, the company's name when calling the template?

Something like get_template_part("template_parts/navbar","Microsoft");

I know the above syntax works for another purpose, I'm just trying to illustrate what I want to achieve.

Is there a way to do this? Thanks beforehand!

Share Improve this question asked Feb 3, 2019 at 12:15 HimadHimad 5552 silver badges9 bronze badges 1
  • 2 Possible duplicate of Passing variables from header.php to template and vice verca – Jacob Peattie Commented Feb 3, 2019 at 12:26
Add a comment  | 

1 Answer 1

Reset to default 0

You can include the template like this include(locate_template('template-part.php'));. to pass the variable or used the outer variable/ dynamic values in the template.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748947602a315065.html

最新回复(0)