首页 > 解决方案 > 嗨,关于 wordpress require_once

问题描述

我想分离我的文件并通过不将它们放在function.php中来组织它我这样做了。

function required_theme_files() {
	require_once( get_template_directory() . '/inc/customize/color.php' );
	require_once( get_template_directory() . '/inc/sidebar.php' );
}
add_action( 'init', 'required_theme_files' );

但sidebar.php 根本不起作用似乎不知道问题我不是真正的程序员,更像是前端设计师。

function ounox_widget_setup() {
   
	register_sidebar(
		array(
			'name' => 'Sidebar',
			'id' => 'sidebar-1',
			'class' => 'custom',
			'description' => 'Standard Sidebar',
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h6 class="widget-title">',
			'after_title'   => '</h6>',
		)
	);

}
add_action( 'widgets_init','ounox_widget_setup' );

标签: phpwordpress

解决方案


尝试 Debug 并查看您遇到了哪些 PHP 错误,例如sidebar.php未加载或未找到。见https://codex.wordpress.org/WP_DEBUG

添加

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false);

在 wp-config.php 和 debug.log 文件将在 wp-content 中。

将“显示”行更改为 true

define( 'WP_DEBUG_DISPLAY', true);

将它们转储到浏览器并记录它们。

<?php1)顶部有开口sidebar.php吗?

2)或者,您的问题可能是使用get_template_directory. 这将返回绝对服务器路径(例如:/home/user/public_html/wp-content/themes/my_theme),而不是 URI。

在使用子主题的情况下,将返回父主题目录的绝对路径。用于get_stylesheet_directory()获取子主题目录的绝对路径。请参阅get_stylesheet_directory_uri() | WordPress 开发人员资源


推荐阅读