如何在程序上&;动态添加CSS&;在PHP中创建wordpress帖子时使用JS

如何在程序上&;动态添加CSS&;在PHP中创建wordpress帖子时使用JS,php,wordpress,custom-wordpress-pages,Php,Wordpress,Custom Wordpress Pages,我正在使用wp_insert_post()函数创建帖子。对于某些帖子类别,我需要在帖子中动态添加CSS和JS。是否有任何内置功能或第三方插件可供我使用 我尝试了wp_enqueue_*函数,使用子主题,但它们将脚本应用于所有帖子,而不考虑类别。您需要为您的类别使用单独的模板文件。看看,您很可能需要使用category-$slug.php模板。在这个模板文件中,您需要使用常规的wp\u enqueue\u xxx函数来注入这个特定类别所需的JS/CSS 更新:要能够将附加的JS/CSS应用于具有特

我正在使用wp_insert_post()函数创建帖子。对于某些帖子类别,我需要在帖子中动态添加CSS和JS。是否有任何内置功能或第三方插件可供我使用


我尝试了wp_enqueue_*函数,使用子主题,但它们将脚本应用于所有帖子,而不考虑类别。

您需要为您的类别使用单独的模板文件。看看,您很可能需要使用
category-$slug.php
模板。在这个模板文件中,您需要使用常规的
wp\u enqueue\u xxx
函数来注入这个特定类别所需的JS/CSS

更新:要能够将附加的JS/CSS应用于具有特定类别的帖子,而不是类别本身,您需要使用函数来检索帖子的类别列表,然后确定列表中是否有必需的类别,如果是,则应用附加资产。它可以看起来像这样:

// Assuming that we have ID of current post inside $postId variable
// that can be retrieved e.g from $postId = the_post()->ID
// or from global $post variable
//
// Also assuming that we have $slug variable that contains slug
// of the category, we need to apply additional JS / CSS to
// 
// Example:
// $postId = 123;
// $slug = 'hairstyle'; 

$categories = wp_get_post_categories($postId);
if (is_array($categories) && array_reduce(array_filter(array_map('get_category', $categories), function ($category) {
        /** @var \WP_Term $category */
        return $category->slug;
    }), function ($found, $category) use ($slug) {
        return $found ?: $category === $slug;
    }, false)) {

    // ... Apply additional JS / CSS ...

}

当然,此代码应该驻留在呈现帖子的模板中,例如在
single.php

中,您需要为您的类别使用单独的模板文件。看看,您很可能需要使用
category-$slug.php
模板。在这个模板文件中,您需要使用常规的
wp\u enqueue\u xxx
函数来注入这个特定类别所需的JS/CSS

更新:要能够将附加的JS/CSS应用于具有特定类别的帖子,而不是类别本身,您需要使用函数来检索帖子的类别列表,然后确定列表中是否有必需的类别,如果是,则应用附加资产。它可以看起来像这样:

// Assuming that we have ID of current post inside $postId variable
// that can be retrieved e.g from $postId = the_post()->ID
// or from global $post variable
//
// Also assuming that we have $slug variable that contains slug
// of the category, we need to apply additional JS / CSS to
// 
// Example:
// $postId = 123;
// $slug = 'hairstyle'; 

$categories = wp_get_post_categories($postId);
if (is_array($categories) && array_reduce(array_filter(array_map('get_category', $categories), function ($category) {
        /** @var \WP_Term $category */
        return $category->slug;
    }), function ($found, $category) use ($slug) {
        return $found ?: $category === $slug;
    }, false)) {

    // ... Apply additional JS / CSS ...

}

当然,这段代码应该驻留在呈现帖子的模板中,例如在
single.php

中,我有从标题动态生成的slug。我是否需要为创建的每个帖子创建每个php文件?还有更简单的方法吗?@mallik1055这不是一个后段塞,而是一个类别塞。或者,您可以使用category id,但从我的角度来看,这不太方便。创建了category-hairstyles.php并编写了一些代码。代码反映在“example.com/hairstyles”页面上,但不反映“hairstyle”类别的帖子。我遗漏了什么吗?@mallik1055抱歉,看起来我误解了你问题中的“某些帖子类别…”。我将更新answerI,我有从标题动态生成的slug。我是否需要为创建的每个帖子创建每个php文件?还有更简单的方法吗?@mallik1055这不是一个后段塞,而是一个类别塞。或者,您可以使用category id,但从我的角度来看,这不太方便。创建了category-hairstyles.php并编写了一些代码。代码反映在“example.com/hairstyles”页面上,但不反映“hairstyle”类别的帖子。我遗漏了什么吗?@mallik1055抱歉,看起来我误解了你问题中的“某些帖子类别…”。我会更新答案