Support » Fixing WordPress » Embedding CSS using custom fields – not working

  • Hi,

    I am trying to embed some css into specific posts only, using custom fields.

    The specific posts are custom post types, but I am finding it impossible to embed css in regular or custom posts.

    There are a number of simple tutorials out there on the subject, for instance:, which I have followed.

    In header.php, between the <head></head> tags, I have the following code:

    <?php if (get_post_type() == 'photogallery') {
        $css = get_post_meta($post->ID, 'css', true);
        if (!empty($css)) { ?>
            <style type="text/css">
            <?php echo $css; ?>
        <?php }
    } ?>

    In the custom field of my post, I have entered the name ‘css’, and the value:#photo{color:red}

    FYI, custom fields are enabled for custom post type ‘photogallery’ as follows in functions.php:

    add_action( 'init', 'create_my_post_types' );
    function create_my_post_types() {
    	register_post_type( 'photogallery',
    			'labels' => array(
    				'name' => __( 'photogallery' ),
    				'singular_name' => __( 'photogallery' )
    			'public' => true,
    			'supports' => array( 'title', 'editor', 'custom-fields' ),

    With all this code in place, the css is not working, or even showing in the source code.

    Does anyone know what I’m doing wrong?



Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi There,

    Just wondering if anyone can please point me in the right direction with this one.

    I’m not sure if the code is wrong, or if i should be trouble shooting other areas. I have googled the hell out of it, and I can’t find an answer.

    Thanks in advance,




    Forum Moderator

    if there is no output, i would assume that the conditional statement is not true:
    if (get_post_type() == 'photogallery')

    you could try this by echoing the result of ‘get_post_type()’, or you could try to add $post->ID

    if (get_post_type() == 'photogallery')
    if (get_post_type($post->ID) == 'photogallery')


    if (get_post_type($post->ID) == 'photogallery')

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Embedding CSS using custom fields – not working’ is closed to new replies.