最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Enqueue script only on child pages of custom post types

programmeradmin0浏览0评论

I want to enqueue a script on the child pages of two different custom post types. The custom post types are named "Foods" with post id of 10000, and the other is named "Drinks" with a post ID of 20000. The child pages have a slug of /food/ and /drink/ while the parents have the slug /foods/ and /drinks/.

The code I have in my functions.php file is the following, but it is showing the script on all pages instead of just the child pages of the custom post types. What am I doing incorrectly?

function addScript() {
  global $post;

  if ($post->post_parent_in(array('10000','20000'))) {
    wp_enqueue_script('load_script');
  }
}

addScript();

I want to enqueue a script on the child pages of two different custom post types. The custom post types are named "Foods" with post id of 10000, and the other is named "Drinks" with a post ID of 20000. The child pages have a slug of /food/ and /drink/ while the parents have the slug /foods/ and /drinks/.

The code I have in my functions.php file is the following, but it is showing the script on all pages instead of just the child pages of the custom post types. What am I doing incorrectly?

function addScript() {
  global $post;

  if ($post->post_parent_in(array('10000','20000'))) {
    wp_enqueue_script('load_script');
  }
}

addScript();

Share Improve this question edited Feb 6, 2020 at 1:54 CUH asked Feb 6, 2020 at 1:42 CUHCUH 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Issues

The code you shared has some issues: First of all calling a function like this won't work on the bootstrapping process of the WordPress and can't get necessary values where and when necessary, so the conditions might not work as you expected.

Solution: Hook the function to an appropriate hook, and wp_enqueue_scripts is such a hook to work with enqueued scripts.

You used $post->post_parent_in out of the context. post_parent_in is an attribute of the WP_Query() class, that is not available in the global $post object.

Solution: Instead, you have post_parent key with necessary values to compare with.

Try out

The following code will [technically] enqueue myscript to only on the child pages. The inline comments will guide you for what's the purpose of certain lines. (Not thoroughly tested)

/**
 * My Custom Script for Child Pages only.
 */
function wpse357977_custom_script() {
    // If not on the detail page, bail out.
    if(! is_singular()) {
        return;
    }

    global $post;
    // If not on my Custom Post Type, bail out.
    if('mycpt' !== $post->post_type) {
        return;
    }


    // '0 !==' means only on the child pages.
    if( 0 !== $post->post_parent) {
        wp_register_script('myscript', get_template_directory_uri() .'/myscript.js', array(), 'wpse357977', true);
        wp_enqueue_script('myscript');
    }

}

add_action( 'wp_enqueue_scripts', 'wpse357977_custom_script' );
发布评论

评论列表(0)

  1. 暂无评论