• Resolved Sykat

    (@sykat)


    I am really stacked. I created a custom post template for static homepage so that I can free edit it in backend and still editing it. But I want to use an extra css file if a page/post is pointed at that post type.
    So, how to add a code in header that will only be shown when post/page is a specific post template?

Viewing 6 replies - 1 through 6 (of 6 total)
  • If a page uses a template created for a custom post, it will have a body class that’s unique to that template.

    For example, let’s say you created a custom post type called “movie”. Then every time you show a page with information about a movie, it will use the template single-movie.php (if it exists), and the page will have a body class called single-movie.

    So instead of calling a CSS file only when you’re on that page, you can add classes to your generic CSS file, that are all under the single-movie class.

    For example:

    body.single-movie {
        background:red;
    }
    
    body.single-movie p {
        color:green;
    }

    Now all pages that use the single-movie.php template will have a green background and red paragraph text.

    Thread Starter Sykat

    (@sykat)

    thank you. But the thing I am caring about my site i blazing speed.. I am making a theme that only have the functions and ability that my site requires only. If I do your trick then it may be ok but I don’t want those css for my other pages. So why increase css file size?

    And what if I want to add a js, extra meta tags or something in header?
    As an example, onee jquery for gallery post types?

    Maybe you’re after something like this then (untested, but should get the idea across):

    function load_some_stylesheet() {
      if ( is_page_template('single-movie.php') ) {
    	wp_register_style('someStylesheet', get_template_directory_uri() . '/css/some-custom-css.css' );
    	wp_enqueue_style('someStylesheet');
      }
    }
    
    add_action( 'wp_enqueue_scripts', 'load_some_stylesheet' );
    Thread Starter Sykat

    (@sykat)

    I am actually looking something like this (Based on ur code and untested)

    function load_some_header() {
      if ( is_page_template('single-album.php') ) {
    echo '<script>Codes goes here</script>'; //that should add some code in header
    }
    }
    add_action( 'wp_enqueue_scripts', 'load_some_stylesheet' );

    But I wonder load_some_header() is existed or not.. but there are some way. I actually adding a slider. I coded the slider in a file like (theme
    )/mods/slider.php and added it in (theme)/cmhome.php (that is my homepage post template.)
    I like to add the codes (that add something in header) in slider.php than header.php.
    An simple example can clear it. What i have to do with slider.php to add <script></script> within <head>?

    If you only want to load an extra CSS file (as per your original post) I would still recommend using wp_register_style, and if you just want to load extra Javascript, it would be wp_register_script.

    If you want to load actual HTML markup then you could go with include (to call a PHP file).

    Regardless, I wouldn’t go with ECHO functions.

    Thread Starter Sykat

    (@sykat)

    understood. though this is not the proper solution I can go with it. And I will examine a plugin (like Youst seo) or theme that can add custom codes in header to get the code.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to add contents in wp header for a custom post template?’ is closed to new replies.