个性化阅读
专注于IT技术分析

如何在子主题上编辑函数?

我在function.php的主要主题上具有此函数:

if(!function_exists('eagle_booking_append_booking_button_menu') && eagle_booking_get_option('eagle_booking_header_button')):

    add_filter( 'wp_nav_menu_items', 'eagle_booking_append_booking_button_menu', 12, 2 );

    function eagle_booking_append_booking_button_menu ( $items, $args ) {

        // BUTTON ACTION BASED ON BOOKING SYSTEM
        if (eagle_booking_get_option('booking_type') == 'builtin' ) {
          $eagle_booking_button_action  = eagle_booking_search_page();

        } elseif (eagle_booking_get_option('booking_type') == 'custom') {
          $eagle_booking_button_action = eagle_booking_get_option('booking_type_custom_action');

        } elseif (eagle_booking_get_option('booking_type') == 'booking') {
          $eagle_booking_button_action = eagle_booking_get_option('booking_type_booking_action');

        } elseif (eagle_booking_get_option('booking_type') == 'airbnb') {
          $eagle_booking_button_action = eagle_booking_get_option('booking_type_airbnb_action');

        } elseif (eagle_booking_get_option('booking_type') == 'tripadvisor') {
          $eagle_booking_button_action = eagle_booking_get_option('booking_type_tripadvisor_action');
        }

        if ((eagle_booking_get_option('eagle_booking_header_button')) && ( $args->theme_location == 'zante_main_menu' )) {
            $items .= '<li class="menu_button"><a href="'.$eagle_booking_button_action.'" class="button"><i class="fa fa-calendar"></i>'.esc_html__('BOOK ONLINE', 'eagle-booking').'</a></li>';
        }

        return $items;
    }

endif;

我想编辑函数的最后一部分, 例如:

if ((eagle_booking_get_option('eagle_booking_header_button')) && ( $args->theme_location == 'zante_main_menu' )) {
    $mdpLinkButton = "/contact-us/";
    $items .= '<li class="menu_button"><a href="'.$mdpLinkButton.'" class="button"><i class="fa fa-calendar"></i>'.esc_html__('CONTACT US', 'eagle-booking').'</a></li>';
}

我做了自定义主题。但是我应该在那个function.php中做什么?覆盖上面还是什么?


#1


为此, 你需要删除父主题应用的过滤器, 然后在子主题中再次定义它, 如下所示:

子主题functions.php

add_action( 'init', 'remove_my_action');
function remove_my_action() {
    remove_filter( 'wp_nav_menu_items', 'eagle_booking_append_booking_button_menu', 12 );
}

add_filter( 'wp_nav_menu_items', 'child_eagle_booking_append_booking_button_menu', 12, 2 );

function child_eagle_booking_append_booking_button_menu ( $items, $args ) {

    // BUTTON ACTION BASED ON BOOKING SYSTEM
    if (eagle_booking_get_option('booking_type') == 'builtin' ) {
      $eagle_booking_button_action  = eagle_booking_search_page();

    } elseif (eagle_booking_get_option('booking_type') == 'custom') {
      $eagle_booking_button_action = eagle_booking_get_option('booking_type_custom_action');

    } elseif (eagle_booking_get_option('booking_type') == 'booking') {
      $eagle_booking_button_action = eagle_booking_get_option('booking_type_booking_action');

    } elseif (eagle_booking_get_option('booking_type') == 'airbnb') {
      $eagle_booking_button_action = eagle_booking_get_option('booking_type_airbnb_action');

    } elseif (eagle_booking_get_option('booking_type') == 'tripadvisor') {
      $eagle_booking_button_action = eagle_booking_get_option('booking_type_tripadvisor_action');
    }

    if ((eagle_booking_get_option('eagle_booking_header_button')) && ( $args->theme_location == 'zante_main_menu' )) {
$mdpLinkButton = "/contact-us/";
        $items .= '<li class="menu_button"><a href="'.$mdpLinkButton.'" class="button"><i class="fa fa-calendar"></i>'.esc_html__('CONTACT US', 'eagle-booking').'</a></li>';
    }

    return $items;
}

#2


子主题functions.php在父主题functions.php之前加载, 因此如果父主题函数具有检查(!function_exists()), 则可以在子functions.php中使用相同的函数名称覆盖它。

赞(0)
未经允许不得转载:srcmini » 如何在子主题上编辑函数?

评论 抢沙发

评论前必须登录!