我想在使用当前编辑的帖子(\\u type)/页面的主题中获取模板。
假设我正在编辑一篇文章,然后我想使用模板检索它single.php
如果我正在编辑一个自定义的帖子类型single-{post_type}.php
我想重温一下。
我如何存档?是否存在存储此信息的全局变量?
我知道pages会将其保存为meta\\u数据。
我没有mutch,但我一直在寻找get_query_template
和locate_template
但我需要正确的方向。
谢谢
我想在使用当前编辑的帖子(\\u type)/页面的主题中获取模板。
假设我正在编辑一篇文章,然后我想使用模板检索它single.php
如果我正在编辑一个自定义的帖子类型single-{post_type}.php
我想重温一下。
我如何存档?是否存在存储此信息的全局变量?
我知道pages会将其保存为meta\\u数据。
我没有mutch,但我一直在寻找get_query_template
和locate_template
但我需要正确的方向。
谢谢
基本上,这在admin中不可用。您可以查看我的个人资料,转到我的github页面并获取"current admin info" plugin. 但这对你没有多大帮助。
这将显示模板名称:
get_page_template_slug( get_queried_object_id() );
在前端,您可以使用以下插件,该插件将显示该信息(来自另一个问题)。<?php
/** Plugin Name: (#10537) »kaiser« Get Template file name */
if ( ! class_exists( \'wpse10537_template_name\' ) )
{
add_action( \'plugins_loaded\', array( \'wpse10537_template_name\', \'init\' ) );
class wpse10537_template_name
{
protected static $instance;
public $stack;
public static function init()
{
is_null( self :: $instance ) AND self :: $instance = new self;
return self :: $instance;
}
public function __construct()
{
if ( is_admin() )
return;
add_action( \'wp\', array( $this, \'is_parent_template\' ), 0 );
add_action( \'wp\', array( $this, \'get_template_file\' ) );
add_action( \'template_include\', array( $this, \'get_template_name\' ) );
add_action( \'wp_footer\', array( $this, \'get_template_name\' ), 100 );
}
public function get_template_name( $file )
{
if ( \'template_include\' === current_filter() )
{
$this->to_stack(
"Template file"
,basename( $file )
);
return $file;
}
// Return static var on echo call outside of filter
if (
current_user_can( \'manage_options\' )
AND defined( \'WP_DEBUG\' )
AND WP_DEBUG
)
{
return printf(
\'<div><p style="text-align:center;">%s</p></div>\'
,implode( " – ", $this->stack )
);
}
}
public function get_template_file()
{
if ( ! is_post_type_hierarchical( get_post_type() ) )
return;
$slug = get_page_template_slug( get_queried_object_id() );
if ( ! strstr( $slug, "/" ) )
return $this->to_stack( "Template", $slug );
// Subdirectory info
$this->to_stack(
"Subdirectory"
,strstr( $slug, "/", true )
);
$this->to_stack(
"Template (in subdirectory)"
,str_replace( "/", "", strstr( $slug, "/" ) )
);
}
public function is_parent_template()
{
if ( ! is_null( wp_get_theme()->parent ) )
return $this->to_stack( \'from parent theme\' );
$this->to_stack( \'from current/child theme\' );
}
public function to_stack( $part, $item = \'\' )
{
$this->stack[] = "{$part}: {$item}";
}
} // END Class wpse10537_template_name
} // endif;
如果所有这些都不能作为起点,那么请看看all the related answers.这个答案最初是对我的另一个答案的编辑。稍后我将更正代码
所以我最终得到了一个非常简单的解决方案。这个答案确实让我找到了正确的方向。
我现在不知道这是否是最好的方式,但它满足了我的要求:
if ( $pagenow == \'post.php\' )
{
$template = get_post_meta( $post->ID, \'_wp_page_template\', true );
// Check page-templates, if not set -> page.php
if ( $post->post_type == \'page\' && ( empty( $template ) || \'default\' == $template ) )
{
$template = \'page.php\';
}
elseif( empty( $template ) )
{
// Check single-templates, single-{post_type}.php -> single.php order
if ( file_exists( TEMPLATEPATH .\'/single-\'. $post->post_type .\'.php\' ) ) {
$template = \'single-\'. $post->post_type .\'.php\';
}
elseif ( ( $post->post_type == \'attachment\' || $post->post_type == \'page\' ) && file_exists( TEMPLATEPATH .\'/\'. $post->post_type .\'.php\' ) )
{
$template = $post->post_type .\'.php\';
}
elseif( $post->post_type == \'attachment\' )
{
$template = \'\';
}
else
{
$template = \'single.php\';
}
}
// Read the file
$file = implode( \'\', file( TEMPLATEPATH .\'/\' . $template ) );
// Get the tags in the template, (Blocks Areas: Left, Right)
if ( preg_match_all( \'|Block Areas:(.*)$|mi\', $file, $name ) )
{
$defined_areas = explode( \',\', strtolower( $name[1][0] ) );
$defined_areas = array_map( \'trim\', $defined_areas);
}
}
当尝试登录我的一个WP安装(常规WP 3.4.2-无多站点)时,我一直被重定向到wp-login.php 文件我几乎检查了所有内容:密码正确且已更改。。。没有什么重置为默认主题。。。没有什么已停用所有插件。。。没有什么重新发布了我的wp-login.php 文件没有什么检查.htaccess 文件。。。没有什么wp-config.php 看起来还是一样。删除了我的浏览器数据并使用了其他浏览器。。。还是没什么。通过新的下载安装,完全覆盖了每个WordPress文件。这还能是什么?我没有对代码进行任何升级。