2017-08-02 90 views
0

我正在構建一個WordPress主題。在我的index.php中,我使用get_theme_mod()來獲得一個選項,並且我有一些代碼會根據該選項啓動或不啓動。爲什麼get_theme_mod()拋出一個致命錯誤:調用未定義的函數在wordpress中

<!-- index.php --> 
<?php 
/** 
* The main template file 
* 
*/ 
if (!empty(get_theme_mod('czen_hero'))) { 
    $header_logo = get_theme_mod('czen_hero'); 
} 

if (!empty(get_theme_mod('czen_hero_text'))) { 
    $header_text = get_theme_mod('czen_hero_text'); 
} 

這些選項在functions.php中定義並通過wordpress'Customizer設置。

// HOMEPAGE HEADER 
$wp_customize->add_section('czen_hero_section' , array(
     'title'  => __('Custom Homepage Header', 'coffee-zen'), 
     'priority' => 40, 
     'description' => 'Upload an image and enter some text to create a custom header', 
)); 

$wp_customize->add_setting('czen_hero' , array ('sanitize_callback' => 'czen_sanitize_setting',)); 
$wp_customize->add_setting('czen_hero_text', array ('sanitize_callback' => 'czen_sanitize_setting',)); 

$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'czen_hero', array(
     'label' => __('Header Image', 'coffee-zen'), 
     'section' => 'czen_hero_section', 
     'settings' => 'czen_hero', 
))); 

$wp_customize->add_control('header_text', array(
     'label' => __('Header Text', 'coffee-zen'), 
     'section' => 'czen_hero_section', 
     'settings' => 'czen_hero_text', 
     'type' => 'text', 
)); 

該代碼有效。當這些設置被設置時,html會顯示它應該在的位置。然而,每次我打開在前端一個網頁,我看到這個的時候我檢查:

<!-- index.php --> 
<br /> 
<b>Fatal error</b>: Uncaught Error: Call to undefined function get_theme_mod() in /var/www/vhosts/zentest/wp-content/themes/coffee-zen/index.php:7 
Stack trace: 
#0 {main} 
    thrown in <b>/var/www/vhosts/zentest/wp-content/themes/coffee-zen/index.php</b> on line <b>7</b><br /> 

我可以證實,該功能在/wp-includes/theme.php定義,線855到線879 :

function get_theme_mod($name, $default = false) { 
    $mods = get_theme_mods(); 

    if (isset($mods[$name])) { 
     /** 
     * Filters the theme modification, or 'theme_mod', value. 
     * 
     * The dynamic portion of the hook name, `$name`, refers to 
     * the key name of the modification array. For example, 
     * 'header_textcolor', 'header_image', and so on depending 
     * on the theme options. 
     * 
     * @since 2.2.0 
     * 
     * @param string $current_mod The value of the current theme modification. 
     */ 
     return apply_filters("theme_mod_{$name}", $mods[$name]); 
    } 

    if (is_string($default)) 
     $default = sprintf($default, get_template_directory_uri(), get_stylesheet_directory_uri()); 

    /** This filter is documented in wp-includes/theme.php */ 
    return apply_filters("theme_mod_{$name}", $default); 
} 

所以我的問題是,我怎樣才能防止這種「致命錯誤」?是什麼造成的?我可以使用其他Wordpress核心功能。

我禁用了所有插件,問題依然存在。問題發生在所有主流瀏覽器中。我正在使用WordPress的4.8版本。

在此先感謝。

+0

假設沒有包含'/ wp-includes/theme.php'。 – ficuscr

+0

這聽起來像你的主題的'index.php'文件正在直接加載到某個地方 –

+0

它看起來像我一些ajax-y的東西試圖直接加載索引文件。一個簡單的解決方法是將這些get_theme_mod()位封裝在'function_exists('get_theme_mod')'調用中。 – JakeParis

回答

1

您可以防止一個WordPress主題文件的直接裝載有

if (!defined('ABSPATH')) die(); 

也許通過增加這你的主題索引文件,可以防止錯誤。

+0

添加這個修復它....很奇怪。所以我猜wordpress試圖做一些像直接加載它的東西,或者... –

相關問題