gpt4 book ai didi

php - 在 image.php 中加入 CSS 的条件语句

转载 作者:行者123 更新时间:2023-11-28 08:02:22 25 4
gpt4 key购买 nike

我正在尝试在 WordPress 子主题中创建一个新的附件页面。

我已经创建了 image.php 并且在我的函数中我试图加载一个新的 CSS 文件。我已经有这个用于存档页面并且似乎工作正常。我试过添加页面模板 (image.php) 和附件,但似乎不起作用

if (is_archive()) {
wp_enqueue_style( 'newlayout-style' , get_stylesheet_directory_uri() . '/layouts/archive.css');
} elseif (is_page_template('image.php')) {
wp_enqueue_style( 'newlayout-style' , get_stylesheet_directory_uri() . '/layouts/attachment.css');
} elseif (is_attachment()) {
wp_enqueue_style( 'newlayout-style' , get_stylesheet_directory_uri() . '/layouts/attachment.css');
}

最佳答案

需要理解两点:

1) is_page_template 函数用于确定当前页面模板(如果使用),页面模板与模板层次结构的概念不同。您可以在 codex 中查看更多详细信息:

Page Templates

Template Hierarchy

2) 在 functions.php 中,您需要确定是否显示图像附件,在本例中为 @PieterGoosen已经正确指出,你需要使用is_singleis_attachment模板标签,另外你可以检查附件mime-type是否是图像,所以代码应该是这样的:

function add_css_conditionally() {

if (is_archive()) {
wp_enqueue_style( 'newlayout-style' , get_stylesheet_directory_uri() . '/layouts/archive.css');
} elseif (is_single() && is_attachment()) {
if(stripos($wp_query->post->post_mime_type, 'image') !== false) {
wp_enqueue_style( 'newlayout-style' , get_stylesheet_directory_uri() . '/layouts/attachment.css'); // for image attachments, should be different css file.
} else {
wp_enqueue_style( 'newlayout-style' , get_stylesheet_directory_uri() . '/layouts/attachment.css');
}
}
}
add_filter('wp_enqueue_scripts', 'add_css_conditionally');

关于php - 在 image.php 中加入 CSS 的条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29675367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com