WordPress主题开发中调用after_setup_theme相关函数遇语法错误
反复出现的错误及问题点
你的代码里有3个语法/拼写错误和1个函数使用不当问题,这些直接导致after_setup_theme钩子关联的函数运行报错:
缺少分号导致语法中断
在add_action('after_setup_theme', 'dbpplwp_theme_setup' )这一行末尾未加分号,PHP要求语句必须以分号结尾,这会触发语法错误并中断后续代码执行。函数名称拼写错误1
错误使用了不存在的add_theme_size函数,正确的WordPress图片尺寸添加函数是add_image_size,因此add_theme_size('single-img', 600, 550, array('center','center'))这行代码无效。函数名称拼写错误2
add_thehe_support('automatic-feed-links')里的函数名拼写错误,正确应为add_theme_support(多写了一个h),该错误会导致调用未定义函数,触发致命错误。翻译函数使用不当
register_nav_menus里的_('Primary Menu','dbpplwp')应改用__()(双下划线函数),_()是直接输出翻译内容的函数,而此处需要返回翻译后的字符串用于赋值,__()才是正确选择。
修正后的代码片段:
<?php // theme function code function dbpplwp_theme_setup(){ add_theme_support('custom-logo'); add_theme_support('title-tag'); add_theme_support('post-thumbnails'); add_image_size('home-featured', 680, 400, array('center','center')); add_image_size('single-img', 600, 550, array('center','center')); add_theme_support('automatic-feed-links'); register_nav_menus( array( 'primary' => __('Primary Menu','dbpplwp') )); } add_action('after_setup_theme', 'dbpplwp_theme_setup'); // 补充分号 function dbpplwp_scripts(){ wp_enqueue_style('style',get_stylesheet_uri() ); }; add_action('wp_enqueue_scripts','dbpplwp_scripts'); ?>
内容的提问来源于stack exchange,提问作者codershhoder
相关产品推荐
相关产品推荐

