WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Yukon Cornelius
    Member
    Posted 3 years ago #

    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: 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; ?>
            </style>
        <?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',
    		array(
    			'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?

    Cheers,

    Jimmmy

  2. Yukon Cornelius
    Member
    Posted 3 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,

    Jimmmy

  3. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

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

    http://codex.wordpress.org/Function_Reference/get_post_type

    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')
    or
    if (get_post_type($post->ID) == 'photogallery')

    and

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

Topic Closed

This topic has been closed to new replies.

About this Topic