Forums

get_post_meta help (5 posts)

  1. tyankee
    Member
    Posted 7 months ago #

    I need some help here.. I'm using a theme called 'moneyzine' on moneysinternet.com.

    The theme displays images next to posts. These images are linked via a custom field called 'screen' that you add to your posts.

    The problem comes up with you don't add that custom field to the post, a black image shows up. I'd like a default image to show up.

    The coding that displays the image is in the index.php file under the theme.. It goes like this:

    <div class="entry">
    <?php $screen = get_post_meta($post->ID,'screen', true); ?>
    <?php $screen = get_post_meta($post->ID,'screen', false); ?>
    <img src="<?php echo ($screen); ?>" width="160" height="100" alt="" />
    <?php the_content_limit(600, ""); ?>
    <div class="clear"></div>
    </div>

    what would i change to get this display a default image if no custom image is defined?

  2. mfields
    Member
    Posted 7 months ago #

    Pretty easy fix, just add a bit of logic:

    <div class="entry">
    	<?php
    	$screen = get_post_meta($post->ID,'screen', true);
    	$screen = ( !empty( $screen ) ) ? $screen : 'path/to/default-image.jpg';
    	print '<img src="' . $screen . '" width="160" height="100" alt="" />';
    	the_content_limit(600, "");
    	?>
    	<div class="clear"></div>
    </div>

    Please let me know if you have trouble with this code - it works in theory, but was not tested.

  3. tyankee
    Member
    Posted 7 months ago #

    wow - worked great... thank a lot...

  4. mfields
    Member
    Posted 7 months ago #

    no problem, glad I could help

  5. mrwangkai
    Member
    Posted 6 months ago #


Reply

You must log in to post.

About this Topic