get template part - add a hook with get_template_part()

admin2025-06-03  2

I have this hook. How i can add this hook with get_template_part()? I created a file "myfile.php" where I store the HTML code of my search bar, but a can't manage to connect my template

add_filter('wp_nav_menu_items', 'add_search_form', 10, 2);
function add_search_form($items, $args) {
    if( $args->theme_location == 'MENU-NAME' )
        $items .= '<li class="search"><form role="search" method="get" id="searchform" action="'.home_url( '/' ).'"><input type="text" value="search" name="s" id="s" /><input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" /></form></li>';
    return $items;
}

I have this hook. How i can add this hook with get_template_part()? I created a file "myfile.php" where I store the HTML code of my search bar, but a can't manage to connect my template

add_filter('wp_nav_menu_items', 'add_search_form', 10, 2);
function add_search_form($items, $args) {
    if( $args->theme_location == 'MENU-NAME' )
        $items .= '<li class="search"><form role="search" method="get" id="searchform" action="'.home_url( '/' ).'"><input type="text" value="search" name="s" id="s" /><input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" /></form></li>';
    return $items;
}
Share Improve this question edited Feb 4, 2019 at 14:54 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 4, 2019 at 14:52 AskingAsking 1211 silver badge6 bronze badges 2
  • Are you trying to include myfile.php when you are displaying a certain menu? The question isn't very clear, sorry. – Alexander Holsgrove Commented Feb 4, 2019 at 15:00
  • @Alexander Holsgrove, in my template i inserted the code inside <li>, from $item. And now i try to connect my template with get_template_part(). So i have the next code in my function.php : add_filter('wp_nav_menu_items', 'add_search_form', 10, 2); function add_search_form($items, $args) { if( $args->theme_location == 'MENU-NAME' ) $items .= ; return $items; } ..when i insert in $items = get_template_part('myfile');, it doesn't work. – Asking Commented Feb 4, 2019 at 15:07
Add a comment  | 

2 Answers 2

Reset to default 1

OK, so one simple way would be to edit your template, I assume header.php for where you want the menu to appear. Use the template include to load myfile.php

get_template_part('template-parts/myfile');

Then inside myfile.php:

<?php

$search .= '<li class="search"><form role="search" method="get" id="searchform" action="'.home_url( '/' ).'"><input type="text" value="search" name="s" id="s" /><input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" /></form></li>';

wp_nav_menu(array(
      'menu'            => 'Menu Name',
      'theme_location'  => 'menu-name',
      'container'       => 'nav',
      'container_class' => 'main-menu-class',
      'container_id'    => '',
      'items_wrap'      => '<ul class="navbar-nav">%3$s'. $search .'</ul>'
));

So your myfile contains the menu as well as the search form. This isn't tested, but you get the idea - change out the menu name and HTML elements & classes.

If you want to include an external PHP file inside your template (or code), then put that code inside another template (called, say, mycode.php). Put that mycode.php file in your theme folder.

Then you can get_template_part('mycode'). Note that you do not include the '.php' part as the parameter of get_template_part.

Your code in mycode.php will be executed at the point of the get_template_part call. Note that this is a good way to include an external functions call that is only needed by a template.

Added

More experimentation on how to 'include' a file that has functions inside your template. I learned that the locate_template command is better in all cases. get_template_part may not happen 'soon' enough for your page.

So I now use

locate_template("myfunctions.php",true,true);

where the myfunctions.php is located in the active theme folder. The two 'true' parameters ensure that the file is loaded at the proper time. The first 'true' will load the file if found. The second 'true' will use require_once instead of a require.

My use of this was in a template. I needed some functions that would add things to the head (with an apply_filter('wp_head')). The functions are in the my_functions.php file. So the template contains the locate_template first, then the filters for the wp_head after.

This is a clever and useful way to get a file of your functions to load with a template, while not requiring the functions to be available globally - just in your template.

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

最新回复(0)