Wordpress 和 child 主题 - 找出要覆盖哪个 .php 的策略?
Wordpress & child themes - strategies to find out which .php to override?
从一些基本的 Wordpress Bootstrap 主题开始我想修改 Posts 和 Post 链接的宽度。这比繁重的主题创建更 get-to-know-WP - 我最感兴趣的是如何在不同的主题中应用这样的小定制。我知道每个主题都可以选择它想要提供的 PHP 文件,所以我知道在主题 A 中有效的内容不一定在主题 B 中有效。
我已经为一个主题做了一次,但现在另一个主题没有相同的 PHP 文件名。
例如,我知道要使 posts 的小部件区域变窄,我想覆盖 sidebar.php:
<!-- <div id="secondary" class="widget-area col-md-4" role="complementary"> -->
<div id="secondary" class="widget-area col-lg-2" role="complementary">
并修改 index.php 使 left-side 更宽:
<!-- <section id="primary" class="content-area col-md-8"> -->
<section id="primary" class="content-area col-lg-10">
<div class="tmarker">index.php</div> <!-- I added this! -->
当我在我的网站的根目录时,这工作正常,因为 index.php 正在被使用。
但是,当我导航到 Post 时,我回到了:
<div id="primary" class="content-area col-md-8">
我的 post 区域还没有扩大。这是有道理的——这里没有使用 index.php。
我如何知道哪个 PHP 文件被合成到 post 详细视图中以便我可以覆盖它?
我尝试在页面源代码中寻找 'index.php',但我只看到了我自己添加到 child 覆盖中的那个。
我想我可以在编辑器中查看所有 PHP 并找到所有 class="content-area col-md-8"
并反复试验直到我找到正确的。没什么好玩的。有没有更好的方法?
我可以至少将所有这些 PHP 转储到文件中并为 col-md-8
grep 吗?我需要为主题导出文本。
P.S。我知道 Wordpress child 自定义最好在 style.css 和 functions.php 中完成,但除非我错过了什么,有了 Bootstrap,我想我真的需要为 Bootstrap 完成那些 class
作业来代替工作。
问题 here 解决了类似的问题。
您可以将模板名称存储到全局变量中并将其回显到页脚中。这应该告诉您正在加载什么模板文件(index.php
、page.php
等)。
应将以下代码添加到 functions.php
。
add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
$GLOBALS['current_theme_template'] = basename($t);
return $t;
}
add_action( 'wp_footer', 'get_current_template' );
function get_current_template() {
if( !isset( $GLOBALS['current_theme_template'] ) )
return false;
echo $GLOBALS['current_theme_template'];
}
有很多提示和策略,但其中一个是 "A quick-and-dirty way":
大多数体面的主题都会在正文元素中添加一个 class,以反映正在使用的主题模板。如果您检查 body 元素,您应该能够看到正在使用哪个模板(例如 page-template-default
等)。
如果您的主题分解得比简单的模板级别更进一步,那么您将不得不开始检查给定模板中的代码,以确定哪个代码是 运行,等等。
从一些基本的 Wordpress Bootstrap 主题开始我想修改 Posts 和 Post 链接的宽度。这比繁重的主题创建更 get-to-know-WP - 我最感兴趣的是如何在不同的主题中应用这样的小定制。我知道每个主题都可以选择它想要提供的 PHP 文件,所以我知道在主题 A 中有效的内容不一定在主题 B 中有效。
我已经为一个主题做了一次,但现在另一个主题没有相同的 PHP 文件名。
例如,我知道要使 posts 的小部件区域变窄,我想覆盖 sidebar.php:
<!-- <div id="secondary" class="widget-area col-md-4" role="complementary"> -->
<div id="secondary" class="widget-area col-lg-2" role="complementary">
并修改 index.php 使 left-side 更宽:
<!-- <section id="primary" class="content-area col-md-8"> -->
<section id="primary" class="content-area col-lg-10">
<div class="tmarker">index.php</div> <!-- I added this! -->
当我在我的网站的根目录时,这工作正常,因为 index.php 正在被使用。
但是,当我导航到 Post 时,我回到了:
<div id="primary" class="content-area col-md-8">
我的 post 区域还没有扩大。这是有道理的——这里没有使用 index.php。
我如何知道哪个 PHP 文件被合成到 post 详细视图中以便我可以覆盖它?
我尝试在页面源代码中寻找 'index.php',但我只看到了我自己添加到 child 覆盖中的那个。
我想我可以在编辑器中查看所有 PHP 并找到所有 class="content-area col-md-8"
并反复试验直到我找到正确的。没什么好玩的。有没有更好的方法?
我可以至少将所有这些 PHP 转储到文件中并为 col-md-8
grep 吗?我需要为主题导出文本。
P.S。我知道 Wordpress child 自定义最好在 style.css 和 functions.php 中完成,但除非我错过了什么,有了 Bootstrap,我想我真的需要为 Bootstrap 完成那些 class
作业来代替工作。
问题 here 解决了类似的问题。
您可以将模板名称存储到全局变量中并将其回显到页脚中。这应该告诉您正在加载什么模板文件(index.php
、page.php
等)。
应将以下代码添加到 functions.php
。
add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
$GLOBALS['current_theme_template'] = basename($t);
return $t;
}
add_action( 'wp_footer', 'get_current_template' );
function get_current_template() {
if( !isset( $GLOBALS['current_theme_template'] ) )
return false;
echo $GLOBALS['current_theme_template'];
}
有很多提示和策略,但其中一个是 "A quick-and-dirty way":
大多数体面的主题都会在正文元素中添加一个 class,以反映正在使用的主题模板。如果您检查 body 元素,您应该能够看到正在使用哪个模板(例如 page-template-default
等)。
如果您的主题分解得比简单的模板级别更进一步,那么您将不得不开始检查给定模板中的代码,以确定哪个代码是 运行,等等。