首页 > 解决方案 > 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() );并且没有一个样式通过。

似乎无法弄清楚:

  1. 为什么theme/functions.php会导致 WordPress 错误。
  2. 为什么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';

?>

标签: phpwordpresswordpress-theming

解决方案


你需要使用get_template_directory()你的文件。

并使用new theme_ThemeFunctions下面的类本身。

也用于查看错误:

define('WP_DEBUG', 1);
define('WP_DEBUG_DISPLAY', 1);

推荐阅读