在单个模板页面上放置一些 CSS 样式 - WordPress 插件

发布于 2025-01-14 09:16:43 字数 359 浏览 0 评论 0原文

我正在为实习开发我的第一个 WordPress 插件。

我知道如何通过 wp_enqueue_style() 和 admin_enqueue_scripts 在管理页面上放置样式。我也知道 wp_enqueue_scripts。

我创建了一个自定义帖子类型来存储业务,并在“templates”文件夹中创建了一个名为“single-myCPT.php”的模板页面。

问题是:我不明白如何调用这个单页的css文件。

URL 类似于:mywebsite/myCPT/company

当我使用检查器并进入“网络”时,没有检测到 css 文件。

抱歉我的英语不好,我是一个法国人,想提高英语水平:)。

I'm developing my first WordPress plugin for an internship.

I know how to put style on the admin pages by wp_enqueue_style() and admin_enqueue_scripts. I also know wp_enqueue_scripts.

I created a custom post type to store businesses and a single template page in "templates" folder, called "single-myCPT.php".

The problem is: I don't understand how to call a css file for this single page.

The URL is like : mywebsite/myCPT/company

When I use the inspector and I go on "Network", there is no css file detected.

Sorry for my English, I'm a French guy who wants to improve that language :).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雪碎 2025-01-21 09:16:43

您可以在前端(非管理)页面以及管理页面上使用 wp_enqueue_style() 和 wp_enqueue_script()。

为此,请使用 wp_enqueue_scripts 操作。

function plugin_name_enqueue_frontend_style() {
    wp_enqueue_style( 
            'plugin_name_frontend', 
            plugin_dir_url( __FILE__ ) . 'css/frontend.css');
}

add_action( 'wp_enqueue_scripts', 'plugin_name_enqueue_frontend_style' );

但是,您可能还应该在管理页面上包含前端 css;你可能需要它。

You can use wp_enqueue_style() and wp_enqueue_script() on front-end (non-admin) pages as well as admin pages.

Use the wp_enqueue_scripts action for the purpose.

function plugin_name_enqueue_frontend_style() {
    wp_enqueue_style( 
            'plugin_name_frontend', 
            plugin_dir_url( __FILE__ ) . 'css/frontend.css');
}

add_action( 'wp_enqueue_scripts', 'plugin_name_enqueue_frontend_style' );

But, you should probably also include your frontend css on your admin page; you may need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文