【发布时间】:2011-02-03 09:32:24
【问题描述】:
按照http://josephscott.org/archives/2010/03/database-powered-css-in-wordpress-themes/ 的教程,我正在尝试使用 wordpress 的 parse_request 函数来添加一些 php 驱动的 CSS...主要是在我的主题选项面板中设置的样式选项。我知道我的代码看起来与作者的有点不同,但我已经按照他的方式尝试过了。我可以添加
function kia_wp_head() {
wp_enqueue_style('dynamic', get_bloginfo('stylesheet_directory') . '/admin/ . '?my-custom-content=css');
}
add_action('wp_print_styles', 'kia_wp_head');
//this shows up properly enqueued but when i click on it in source it just brings up a directory listing for the admin folder
function my_custom_wp_request( $wp ) {
if( isset($_GET['my-custom-content']) && $_GET['my-custom-content'] == 'css' ) {
# get theme options
header( 'Content-Type: text/css' ); ?>
body {
background-color: <?php echo 'red'; ?>
}
<?php
exit;
}
}
add_action( 'parse_request', 'my_custom_wp_request' );
但由于背景永远不会变红,我要么没有正确实现这一点,要么本教程缺少关键步骤。我还尝试了将动态 css 放入其自己的 custom-css.php 文件的另一种方法,这是我的最终目标,但我只是想看看我是否可以正确地与解析请求函数交互:
function my_custom_wp_request( $wp ) {
if (
isset($_GET['my-custom-content'])
&& $_GET['my-custom-content'] == 'css'
) {
# get theme options
header( 'Content-Type: text/css' );
require dirname( __FILE__ ) . '/custom-css.php';
exit;
}
}
add_action( 'parse_request', 'my_custom_wp_request' );
这里我不确定 dirname(FILE) 的确切含义,但我也尝试过使用硬编码路径,但也没有用。
那么我如何获得 parse_request 以查看我的 php 驱动样式表?
/* 编辑解决方案 */
基本上这对 wp_enqueue_style 不起作用
wp_enqueue_style('dynamic', get_bloginfo('stylesheet_directory') . '/admin/ . '?my-custom-content=css');
但是通过将样式标签直接插入头部,可以按照 josephscott.org 中的描述工作
. '/admin?my-custom-content=css">【问题讨论】: