Wordpress Custom Field
I wanted to use the custom field for wordpress to have a different header banner for my site here is my code:
<?php
get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="BodyWrap">
<!--MAIN CONT-->
<div id="mainCont">
<?php get_sidebar(); ?>
<div id="rotateBanner"><?php
// check for image
$image = get_post_meta($post->ID, 'image', $single = true);?>
<img class="mainImg" src="<?php bloginfo(template_url); echo $image; ?>" alt=""/>
</div>
<div id="mainCopy">
<div id="content">
<h2><?php single_post_title(); ?></h2>
<?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
Now the code renders but for some reason it only rende开发者_如何学Crs the img path as:
<img alt="" src="http://www.testground.idghosting.com/philcom/wp-content/themes/phil"/>
here is the demosite
in the custom field I put this: image For the value I put this: /images/sampleHead.png
tried it using a php code but there is a plugin that does it well it's workth a try http://wordpress.org/extend/plugins/custom-field-images
I think that you are just missing a "/" - If you look at the code below (a normal way in wordpress to do an image):<img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo.png" alt="logo" /></div>
Therefore you should have <img class="mainImg" src="<?php bloginfo(template_url); ?>/<?php echo $image; ?>
or something similar to this case since I haven't been haven't been able to test it.
You have to make sure inside your add post page, you are creating the correct custom field... if you called your custom field "Image", and your using "image", the Capital in Image would through this off..
and inside the custom field i would a stick in the full URL, direct to the image.. this way its less hassle to code, and easier to link to..
<div id="rotateBanner">
<?php $image = get_post_meta($post->ID, 'image', $single = true);?>
<img class="mainImg" src="<?php echo $image; ?>" alt=""/>
</div>
Your image link should be something like this as bvandrun points out
<img class="mainImg" src="<?php bloginfo(template_url); ?>/<?php echo $image; ?>
I would highly recommend using the MagicFields wordpress plugin for custom fields. Once you have it set up with a page.
精彩评论