【问题标题】:Append data to wordpress get_header()将数据附加到 wordpress get_header()
【发布时间】:2014-01-22 21:55:52
【问题描述】:

在我的 Wordpress 主题中,我有一些带有自定义模板的自定义帖子类型。这些模板使用特定的 CSS 或 Java Script 等附加数据。

有没有办法将这种数据附加到 get_header() 函数中,这样我就可以在不将其硬拷贝到我的自定义帖子类型模板中的情况下对其进行更改?

简而言之,例如。插入 <link rel="stylesheet" href="<?php echo get_bloginfo('template_url'); ?>/css/interview.css" type="text/css" media="all"> 里面

get_header();

【问题讨论】:

    标签: php wordpress append


    【解决方案1】:

    如果我正确理解了这个问题,那么你需要这样的东西:-

    function theme_name_scripts() {
       global $post;
       $post_type = get_post_type( $post );
       if( $post_type === 'your-custom-post-type-slug-here' ) { // edit this line as per requirements
          wp_enqueue_style( 'style-interview', get_bloginfo('template_url') . '/css/interview.css' );     
       }
    }
    add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
    

    感谢@TopDown 在 cmets 中的性能提示。

    您只需将上述代码放在主题的 Functions.php 中。欲了解更多信息:http://codex.wordpress.org/Function_Reference/wp_enqueue_style

    【讨论】:

    • 如果只需要在您的帖子类型上加载,您还可以在上述功能中检查帖子类型以节省 http 性能。全球 $post; $post_type = get_post_type($post);然后根据您的帖子类型 slug 检查 $post_type var。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-05
    • 2019-06-16
    • 2017-09-22
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多