Embedding CSS using custom fields - not working (3 posts)

  1. Yukon Cornelius
    Posted 4 years ago #


    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: http://www.wprecipes.com/how-to-embed-css-in-your-posts-with-a-custom-field, 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?



  2. Yukon Cornelius
    Posted 4 years ago #

    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,


  3. Michael
    Part-Time Forum Moderator
    Posted 4 years ago #

    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')

Topic Closed

This topic has been closed to new replies.

About this Topic