我正试图在我的WordPress插件上包含页面模板文件,以在前端显示"job"自定义帖子类型。
问题是:模板正确加载在自定义帖子类型的单个页面上,但其他页面都是空白的(只显示白色空白页面(,包括主页、wp默认帖子和wp默认页面。
你能告诉我我的错误以及如何解决这个问题吗?非常感谢。
这是代码:
function dwwp_load_templates( $original_template ) {
if ( get_query_var( 'post_type' ) !== 'job' ) {
return;
}
if ( is_archive() || is_search() ) {
if( file_exists( get_stylesheet_directory().'/archive-job.php' ) ){
return get_stylesheet_directory().'/archive-job.php';
}else{
return plugin_dir_path( __FILE__ ).'templates/archive-job.php';
}
} elseif( is_singular('job') ) {
if( file_exists( get_stylesheet_directory().'/single-job.php' ) ){
return get_stylesheet_directory().'/single-job.php';
}else{
return plugin_dir_path( __FILE__ ).'templates/single-job.php';
}
} else {
return get_page_template();
}
return $original_template;
}
add_filter( 'template_include', 'dwwp_load_templates' );
不能只说return
。您必须返回一些模板。修复这条线路
if ( get_query_var( 'post_type' ) !== 'job' ) {
return $original_template;
}
```
为了让模板工作,我们需要能够在调用它们时找到正确的模板。使用以下代码,插件首先在themes/your-theme/woocommerce-plugin-templates/{template-name}
中查找被调用的模板文件,如果没有找到,它将在主主题文件themes/your-theme/{template-name}
中搜索,并返回到plugins/woocommerce-plugin-templates/templates/{template-name}
中的原始插件模板。
<?php
/**
* Locate template.
*
* Locate the called template.
* Search Order:
* 1. /themes/theme/woocommerce-plugin-templates/$template_name
* 2. /themes/theme/$template_name
* 3. /plugins/woocommerce-plugin-templates/templates/$template_name.
*
* @since 1.0.0
*
* @param string $template_name Template to load.
* @param string $string $template_path Path to templates.
* @param string $default_path Default path to template files.
* @return string Path to the template file.
*/
function wcpt_locate_template( $template_name, $template_path = '', $default_path = '' ) {
// Set variable to search in woocommerce-plugin-templates folder of theme.
if ( ! $template_path ) :
$template_path = 'woocommerce-plugin-templates/';
endif;
// Set default plugin templates path.
if ( ! $default_path ) :
$default_path = plugin_dir_path( __FILE__ ) . 'templates/'; // Path to the template folder
endif;
// Search template file in theme folder.
$template = locate_template( array(
$template_path . $template_name,
$template_name
) );
// Get plugins template file.
if ( ! $template ) :
$template = $default_path . $template_name;
endif;
return apply_filters( 'wcpt_locate_template', $template, $template_name, $template_path, $default_path );
}
上面的代码定位并返回一个可以加载并包含在页面上的现有有效路径的路径。为了实际获得模板文件,有一个额外的功能。以下代码可以直接在shortcode函数中用于加载文件。
<?php
/**
* Get template.
*
* Search for the template and include the file.
*
* @since 1.0.0
*
* @see wcpt_locate_template()
*
* @param string $template_name Template to load.
* @param array $args Args passed for the template file.
* @param string $string $template_path Path to templates.
* @param string $default_path Default path to template files.
*/
function wcpt_get_template( $template_name, $args = array(), $tempate_path = '', $default_path = '' ) {
if ( is_array( $args ) && isset( $args ) ) :
extract( $args );
endif;
$template_file = wcpt_locate_template( $template_name, $tempate_path, $default_path );
if ( ! file_exists( $template_file ) ) :
_doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $template_file ), '1.0.0' );
return;
endif;
include $template_file;
}
此代码将使用wcpt_locate_template()
函数查找并返回有效模板文件的路径,当找不到有效模板文件并且插件的模板文件夹中不存在该模板文件时,将显示错误。