php - WordPress - functions.php 导致 WordPress 错误
问题描述
我的主题有以下文件夹结构:
theme
inc
theme
functions.php
init.php
functions.php
在inc/theme/functions.php中,我放置了所有特定于主题的函数(即删除分类法等)。在theme/functions.php中,我拥有所有的核心功能。
使用我当前的代码,WordPress 声明“该站点遇到技术困难。”。如果我删除了theme/functions.php中的所有内容,则会加载内容,但不会执行inc/theme/functions.php中的代码。例如,在inc/theme/functions.php中,我有wp_enqueue_style( 'style', get_stylesheet_uri() );
并且没有一个样式通过。
似乎无法弄清楚:
- 为什么theme/functions.php会导致 WordPress 错误。
- 为什么inc/theme/functions.php没有被执行。
主题/functions.php
<?php
require_once trailingslashit( get_template_directory() ) . 'inc/init.php';
new theme_ThemeFunctions;
class theme_ThemeFunctions {
function __construct() {
load_theme_textdomain( 'theme' );
add_action( 'init', array( $this, 'post_types_taxonomies' ) );
add_action( 'init', array( $this, 'register_menus' ) );
}
public function post_types_taxonomies() {
register_post_type(
'case-studies',
build_post_args(
'case-study', 'Case study', 'Case studies',
array(
'menu_icon' => 'dashicons-book',
'menu_position' => 20,
'has_archive' => true
)
)
);
}
public function register_menus() {
register_nav_menus(
array(
'main' => __( 'Main Menu', 'theme' ),
)
);
}
}
?>
公司/主题/functions.php
<?php
function scriptAndStyles() {
wp_enqueue_style( 'style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'scriptAndStyles' );
function remove_editor() {
remove_post_type_support('page', 'editor');
}
add_action('admin_init', 'remove_editor');
// Remove featured image option from pages
function remove_thumbnail_box() {
remove_meta_box( 'postimagediv','page', 'side' );
}
add_action('do_meta_boxes', 'remove_thumbnail_box');
// Remove posts type option
function post_remove () {
remove_menu_page('edit.php');
}
add_action('admin_menu', 'post_remove');
?>
公司/init.php
<?php
$include_dir = trailingslashit( get_template_directory() ) . 'inc/';
// Load any custom functions
require_once $include_dir . 'theme/functions.php';
?>
解决方案
你需要使用get_template_directory()
你的文件。
并使用new theme_ThemeFunctions
下面的类本身。
也用于查看错误:
define('WP_DEBUG', 1);
define('WP_DEBUG_DISPLAY', 1);
推荐阅读
- jquery - 用户在新浏览器选项卡中输入 PDF 文件的 HTML 表单
- android - VideoPlayer 小部件不断给出 NullPointerException 调用空对象引用上的虚拟方法
- vba - 用于自动搜索的 Excel VBA 代码
- javascript - 用数字和星号在javascript中构建正方形
- arrays - 选择多个输入并以角度 5 存储到数组中
- php - 我无法在 PHP 邮件程序中编辑我的邮件文本
- mysql - 错误代码:1292。截断不正确的 DOUBLE 值
- c++ - 为字典 cpp 创建 []= 运算符
- jquery - 根据列组移动DataTable colmn和列在组内移动
- php - smarty 中的 preg_replace 用于删除 unicode 符号