ThemeShaper Forums » Thematic

[closed]

Include a style sheet and some codes if a custom field exsits

(9 posts)
  • Started 3 years ago by sheep
  • Latest reply from sheep
  • This topic is resolved
  1. sheep
    Member

    Hi, the title says it all. I would like to include a css style sheet to the header and some code to the footer when a custom field value exists.

    I've tried the following code ->

    for the css

    function add_css() { ?>
    
    <?php if ( get_post_meta($post->ID, 'fullbg', true) ) : ?>
    <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/thestylesheetiwant.css" />
    <?php endif; ?>
    
    <?php }
    add_action('wp_head', 'add_css');

    and for the footer

    function add_footer() { ?>
    
    <?php if ( get_post_meta($post->ID, 'fullbg', true) ) : ?>
    <img class="bg" src="<?php echo get_post_meta($post->ID, 'fullbg', true) ?>" alt="<?php the_title(); ?>" />
    <?php endif; ?>
    
    <?php }
    add_action('thematic_after', 'add_footer');

    but it's not working. help would be greatly appreciated

    Posted 3 years ago #
  2. are any parts showing up in the source code? also i'm not sure at ALL but does $post->ID work outside of a loop? the footer image part looks correct, so if that isn't getting inserted at all that will tell you that your IF condition is always returning false.... and that is probably b/c $post-ID has no value. try $postID = get_the_ID(); and then use $postID in its place.

    check out this article on how to conditionally switch stylesheets. you should be able to adapt it to your purpose.

    http://programming.thematic4you.com/2010/01/how-to-use-a-different-layout-for-a-certain-page/

    Posted 3 years ago #
  3. sheep
    Member

    No nothing shows up on the source code, and the tutorial on thematic4you did not work either. I am convinced that there is something wrong with the if statement. I am a php noob so I don't know how to fix it.

    the following works (is adds the style sheet if it's single)

    function add_css() { ?>
    
    <?php if ( is_single()) : ?>
    <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/thestylesheetiwant.css" />
    <?php endif; ?>
    
    <?php }
    add_action('wp_head', 'add_css');

    but the following does not

    function add_css() { ?>
    
    <?php if ( get_post_meta($post->ID, 'fullbg', true) ) : ?>
    <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/thestylesheetiwant.css" />
    <?php endif; ?>
    
    <?php }
    add_action('wp_head', 'add_css');

    does anyone know how to write an if statement that says

    if a value exists for the custom field "fullbg"

    Posted 3 years ago #
  4. did you try what i mentioned about get_the_ID() ?

    Posted 3 years ago #
  5. sheep
    Member

    I don't know what get_the_ID is, but the following code works if i include it directly in the header.php

    <?php  if (get_post_meta($post->ID, "fullbg", true)) { ?>
    	<link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/ie/ie7.css" />
    <?php } ?>

    how can i do the same thing in the function.php ?

    Posted 3 years ago #
  6. sheep
    Member

    and yes, I tried the get_the ID, with the following code.

    function add_css() { ?>
    
    <?php $postID = get_the_ID(); if (get_post_meta($postID->ID, "fullbg", true)) { ?>
    <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/ie/ie7.css" />
    <?php } ?>
    
    <?php }
    add_action('wp_head', 'add_css');

    still no avail

    Posted 3 years ago #
  7. This is an issue of variable scope: http://php.net/manual/en/language.variables.scope.php

    I think this example is more what you're trying for. Notice the is_single() , otherwise the stylesheet would load on index pages as well.

    function add_css() { ?>
    	<?php global $post; ?>
    	<?php if ( is_single() && get_post_meta($post->ID, 'fullbg', true) ) : ?>
    
    	<link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/thestylesheetiwant.css" />
    
    	<?php endif; ?>
    <?php }
    
    add_action('wp_head', 'add_css');
    Posted 3 years ago #
  8. well get_the_ID didn't work b/c you didn't USE it in your if statement.

    $postID = get_the_ID(); if (get_post_meta($postID, "fullbg", true))

    no guarantee that would've worked either, but as you had it you weren't doing anything different from your first post.

    i think gene has nailed it tho.

    Posted 3 years ago #
  9. sheep
    Member

    It worked! Thank you both so much for the help!

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.