dongqian3198 2017-08-02 19:28
浏览 75
已采纳

为什么get_theme_mod()抛出致命错误:在wordpress中调用未定义的函数

I am building a wordpress theme. In my index.php I use get_theme_mod() to get an option and I have some code that will or will not fire depending on that option.

<!-- 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' );
}

These options are defined in functions.php and set via 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',
) );

The code works. When those settings are set, the html shows up where it is supposed to. However, every time I load a page on the front end, I see this when I inspect:

<!-- 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 />

I can confirm that the function IS defined in /wp-includes/theme.php, line 855 to line 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 );
}

So my question is, how can I prevent this "Fatal Error"? What is causing it? I can use other Wordpress core functions fine.

I disabled all plugins, issue persists. Issue occurs in all major browsers. I am using wordpress version 4.8.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanlian1320 2017-08-02 19:43
    关注

    You can prevent the direct loading of a WordPress theme file with

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

    Perhaps by adding this to your theme's index file, you can prevent the error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能