下面的代码检索当前显示内容的自定义帖子类型,然后决定是否打印样式表。一切都取决于您的页面名称。你也可以var_dump($ctp_obj_name);
或printr($ctp_obj_name);
然后查看自定义post类型对象还提供了什么,并决定使用什么来代替名称。
您的函数中应该包含以下内容。php或其中包含的某些文件:
add_action( \'init\', \'print_squeeze_style\' );
function print_squeeze_style() {
define( \'YOUR_PATH\', get_bloginfo(\'stylesheetpath\').\'/squeeze_css_dir/\' ); // define css folder for squeeze here
$post_type = \'your_custom_post_type\'; // define custom post type name here
// ID of post type currently displayed
$ctp_ID = get_the_ID();
// retrieve the whole ctp object of the current "post"
$ctp_obj = get_post_type( $ctp_ID );
$ctp_obj_name = $ctp_obj->name; // get name
$ctp_obj_type = $post->post_type; // type
// file name depending on Custom Post Type Page Name
if ( $ctp_obj_name == \'page name A\' ) {
$file = \'filename_a.css\';
}
elseif ( $ctp_obj_name == \'page name B\' ) {
$file = \'filename_b.css\';
}
else {
$file = \'filename_default.css\';
}
// Register the stylesheet for printing
if ( post_type_exists( $post_type ) )
wp_register_style( \'squeeze-css\', YOUR_PATH.$file, false, \'0.0\', \'screen\' );
// print styles depending on Custom Post Type Page Name
// Then output/print style in head
if ( $ctp_obj_type == $post_type ) {
add_action( \'wp_head\', wp_print_styles( \'squeeze-css\' ) );
}
}
如下文所述(请也更新之前的答案),您需要在body\\u class()函数输出上使用一个过滤器来执行以下操作
body.squeeze div.wrapper div.post .some_element_class
add_filter( \'body_class\', \'body_class_for_squeeze\' );
function body_class_for_squeeze( $classes ) {
$post_type = \'your_custom_post_type\'; // define custom post type name here
// ID of post type currently displayed
$ctp_ID = get_the_ID();
// retrieve the whole ctp object of the current "post"
$ctp_obj = get_post_type( $ctp_ID );
$ctp_obj_name = $ctp_obj->name; // get name
$ctp_obj_type = $post->post_type; // type
// abort if we\'re not on a squeeze page
if ( $ctp_obj_type !== $post_type )
return;
$classes[] .= \'squeeze\';
return $classes;
}